modify readme, add example

This commit is contained in:
wangjian 2022-07-13 17:14:38 +08:00
parent 58a846e055
commit aa856039ae
2 changed files with 33 additions and 15 deletions

View File

@ -12,24 +12,32 @@ package main
import( import(
"git.hpds.cc/Component/logging" "git.hpds.cc/Component/logging"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zap"
) )
func main(){ func main(){
opt := Options{ logger := LoadLoggerConfig()
Path : "./log/",
Prefix : "",
ErrorFileSuffix: "",
WarnFileSuffix : "",
InfoFileSuffix : "",
DebugFileSuffix: "",
Level : zapcore.DebugLevel,
MaxSize : 100,
MaxBackups : 1000,
MaxAge : 30,
Development : true,
}
logger := logging.NewLogger(opt)
logger.Info("this is a test log") 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"]),
)
}
``` ```

View File

@ -35,6 +35,7 @@ var (
errWS, warnWS, infoWS, debugWS zapcore.WriteSyncer // IO输出 errWS, warnWS, infoWS, debugWS zapcore.WriteSyncer // IO输出
debugConsoleWS = zapcore.Lock(os.Stdout) // 控制台标准输出 debugConsoleWS = zapcore.Lock(os.Stdout) // 控制台标准输出
errorConsoleWS = zapcore.Lock(os.Stderr) errorConsoleWS = zapcore.Lock(os.Stderr)
LogLevel = make(map[string]zapcore.Level)
) )
type Logger struct { type Logger struct {
@ -99,6 +100,15 @@ func (logger *Logger) init() {
if err != nil { if err != nil {
panic(err) 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() defer logger.Logger.Sync()
} }