1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package logger
- import (
- rotatelogs "github.com/lestrrat-go/file-rotatelogs"
- "io"
- "os"
- "time"
- //"gogs.baozhida.cn/zoie/OAuth-core/debug/writer"
- "gogs.baozhida.cn/zoie/OAuth-core/logger"
- log "gogs.baozhida.cn/zoie/OAuth-core/logger"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- "gogs.baozhida.cn/zoie/OAuth-core/plugins/logger/zap"
- )
- // SetupLogger 日志 cap 单位为kb
- func SetupLogger(opts ...Option) logger.Logger {
- op := setDefault()
- for _, o := range opts {
- o(&op)
- }
- if !pkg.PathExist(op.path) {
- err := pkg.PathCreate(op.path)
- if err != nil {
- log.Fatalf("create dir error: %s", err.Error())
- }
- }
- var err error
- var output io.Writer
- switch op.stdout {
- case "file":
- //output, err = writer.NewFileWriter(
- // writer.WithPath(op.path),
- // writer.WithCap(op.cap<<10),
- //)
- //if err != nil {
- // log.Fatal("logger setup error: %s", err.Error())
- //}
- output, err = rotatelogs.New(
- op.path+"/%Y-%m-%d.log",
- rotatelogs.WithLinkName(op.path+"/log.log"),
- rotatelogs.WithMaxAge(time.Duration(op.maxDays)*24*time.Hour),
- rotatelogs.WithRotationTime(24*time.Hour),
- )
- if err != nil {
- log.Fatal("logger setup error: %s", err.Error())
- }
- default:
- output = os.Stdout
- }
- var level logger.Level
- level, err = logger.GetLevel(op.level)
- if err != nil {
- log.Fatalf("get logger level error, %s", err.Error())
- }
- switch op.driver {
- case "zap":
- log.DefaultLogger, err = zap.NewLogger(logger.WithLevel(level), logger.WithOutput(output), zap.WithCallerSkip(2))
- if err != nil {
- log.Fatalf("new zap logger error, %s", err.Error())
- }
- //case "logrus":
- // setLogger = logrus.NewLogger(logger.WithLevel(level), logger.WithOutput(output), logrus.ReportCaller())
- default:
- log.DefaultLogger = logger.NewLogger(logger.WithLevel(level), logger.WithOutput(output))
- }
- return log.DefaultLogger
- }
|