package
1.6.23
Repository: https://github.com/mittacy/ego.git
Documentation: pkg.go.dev

# README

log

zap框架二次封装

使用

func main() {
    // 新建句柄
    field := zapcore.Field{
        Key:    "module_name",
        Type:   zapcore.StringType,
        String: "serverName",
    }
    bizLog := New("default",
        WithPath("./storage/logs"),
        WithTimeFormat("2006-01-02 15:04:05"),
        WithLevel(zapcore.InfoLevel),
        WithPreName("biz_"),
        WithEncoderJSON(false),
        WithFields(field))
    
    // 记录日志
    bizLog.Debug("this is SugarDebug")
    bizLog.Info("this is SugarInfo")
    bizLog.Warn("this is SugarWarn")
    bizLog.Error("this is SugarError")
    
    bizLog.Debugf("this is %s", "Debugf")
    bizLog.Infof("this is %s", "Infof")
    bizLog.Warnf("this is %s", "Warn")
    bizLog.Errorf("this is %s", "Errorf")
    
    bizLog.Debugw("this is Debugw", "k", "Debugw")
    bizLog.Infow("this is Infow", "k", "Infow")
    bizLog.Warnw("this is Warnw", "k", "Warnw")
    bizLog.Errorw("this is Errorw", "k", "Errorw")
}


全局配置

修改默认配置,之后新建的日志,将默认使用这些配置

SetGlobalConf(options ...ConfigOption), 配置项使用WithXxx()

  • WithPath 日志路径,默认为 .
  • WithLevel 最低打印级别,默认为 debug
  • WithLogInConsole 是否打印到控制台,默认为 false
  • WithFields 该日志都加的字段,默认为空
  • WithEncoderJSON 是否为json格式日志,默认为 true
  • WithTimeFormat 时间格式,默认为 2006-01-02T15:04:05Z07:00
  • WithPreName 日志前缀,默认为 biz_
func main() {
    field := zapcore.Field{
        Key:    "module_name",
        Type:   zapcore.StringType,
        String: "helloLog",
    }
    // 设置默认配置后,之后新建的所有日志都默认使用该配置
    SetDefaultConf(
    	WithPath("./storage/logs"),
        WithTimeFormat("2006-01-02 15:04:05"),
        WithLevel(zapcore.InfoLevel),
        WithPreName("biz_"),
        WithEncoderJSON(false),
        WithFields(field))
    
    // 新建日志
    // ……
}

带有请求id的日志

func main() {
    k := "request_id"
    c := context.WithValue(context.Background(), k, "r61f0ed0d70098_Zw8R1aoyl4tGeB4HMV")
    
    // 告知log如何从上下文获取请求id
    SetDefaultConf(WithRequestIdKey(k))
    
    l := New("trace")
    l.DebugWithTrace(c,"this is SugarDebug")
    l.InfoWithTrace(c,"this is SugarInfo")
    l.WarnWithTrace(c,"this is SugarWarn")
    l.ErrorWithTrace(c,"this is SugarError")
    
    l.DebugfWithTrace(c,"this is %s", "Debugf")
    l.InfofWithTrace(c,"this is %s", "Infof")
    l.WarnfWithTrace(c,"this is %s", "Warn")
    l.ErrorfWithTrace(c,"this is %s", "Errorf")
    
    l.DebugwWithTrace(c,"this is Debugw", "k", "Debugw")
    l.InfowWithTrace(c,"this is Infow", "k", "Infow")
    l.WarnwWithTrace(c,"this is Warnw", "k", "Warnw")
    l.ErrorwWithTrace(c,"this is Errorw", "k", "Errorw")
}

# Functions

New 创建新日志文件句柄,使用默认配置 @param name 日志名 @param options 日志配置,将覆盖默认配置 @return *Logger.
SetDefaultConf 设置默认日志配置 @param options.
WithEncoderJSON 是否设置为json格式日志 @param isJSONEncoder @return ConfigOption.
WithFields 添加全局日志的新字段, 新建的日志将会是新配置,已经建立的日志配置不变 @param field 日志字段.
WithLevel 设置服务记录的最低日志级别 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param l 日志级别(-1:debug、0:info、1:warn、2:error).
WithLogInConsole 是否输出到控制台 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param isLogInConsole.
WithPath 设置日志路径, 修改后,新建的日志将会是新配置,已经建立的日志配置不变 @param path 路径.
WithPreName 设置日志前缀 @param pre 前缀 @return ConfigOption.
WithRequestIdKey 设置请求上下文请求id键名,记录日志时,将从上下文中取请求id并记录 @param k 键名 @return ConfigOption.
WithTimeFormat 设置时间格式 @param format 时间格式 @return ConfigOption.

# Structs

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author