diff --git a/README.md b/README.md index 51c565c..46b2404 100644 --- a/README.md +++ b/README.md @@ -12,24 +12,32 @@ package main import( "git.hpds.cc/Component/logging" - "go.uber.org/zap/zapcore" + "go.uber.org/zap/zap" ) func main(){ - opt := Options{ - Path : "./log/", - Prefix : "", - ErrorFileSuffix: "", - WarnFileSuffix : "", - InfoFileSuffix : "", - DebugFileSuffix: "", - Level : zapcore.DebugLevel, - MaxSize : 100, - MaxBackups : 1000, - MaxAge : 30, - Development : true, - } - logger := logging.NewLogger(opt) + logger := LoadLoggerConfig() logger.Info("this is a test log") + //也可以这样直接使用 + + logging.L().Info("this is a test log") } + +// LoadLoggerConfig 加载日志配置 +func LoadLoggerConfig() *logging.Logger { + return logging.NewLogger( + logging.SetPath("./log/"), + logging.SetPrefix(""), + logging.SetDevelopment(true), + logging.SetDebugFileSuffix(""), + logging.SetWarnFileSuffix(""), + logging.SetErrorFileSuffix(""), + logging.SetInfoFileSuffix(""), + logging.SetMaxAge(30), + logging.SetMaxBackups(100), + logging.SetMaxSize(100), + logging.SetLevel(logging.LogLevel["debug"]), + ) +} + ``` \ No newline at end of file diff --git a/logger.go b/logger.go index 4cde38e..00bd219 100644 --- a/logger.go +++ b/logger.go @@ -35,6 +35,7 @@ var ( errWS, warnWS, infoWS, debugWS zapcore.WriteSyncer // IO输出 debugConsoleWS = zapcore.Lock(os.Stdout) // 控制台标准输出 errorConsoleWS = zapcore.Lock(os.Stderr) + LogLevel = make(map[string]zapcore.Level) ) type Logger struct { @@ -99,6 +100,15 @@ func (logger *Logger) init() { if err != nil { panic(err) } + + LogLevel["debug"] = zap.DebugLevel + LogLevel["info"] = zap.InfoLevel + LogLevel["warn"] = zap.WarnLevel + LogLevel["error"] = zap.ErrorLevel + LogLevel["dpanic"] = zap.DPanicLevel + LogLevel["panic"] = zap.PanicLevel + LogLevel["fatal"] = zap.FatalLevel + defer logger.Logger.Sync() }