1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package log
- import (
- "cockData/server/config"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- "os"
- )
- var _logger *zap.SugaredLogger
- func Init(config *config.LoggerConfig) {
- atomicLevel := initAtomicLevel(config.Level)
- encoderConfig := zap.NewProductionEncoderConfig()
- encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
- encoder := zapcore.NewConsoleEncoder(encoderConfig)
- var zapCore zapcore.Core
- consoleSyncer := zapcore.AddSync(os.Stdout)
- if config.Lumberjack != nil {
- writerSyncer := zapcore.AddSync(config.Lumberjack)
- zapCore = zapcore.NewTee(
- zapcore.NewCore(encoder, writerSyncer, atomicLevel),
- zapcore.NewCore(encoder, consoleSyncer, atomicLevel),
- )
- } else {
- zapCore = zapcore.NewCore(encoder, consoleSyncer, atomicLevel)
- }
- logger := zap.New(zapCore)
- _logger = logger.Sugar()
- _logger.Info("日志管理器初始化完毕...")
- }
- func initAtomicLevel(level string) zap.AtomicLevel {
- atomicLevel := zap.NewAtomicLevel()
- switch level {
- case "DEBUG":
- atomicLevel.SetLevel(zapcore.DebugLevel)
- case "INFO":
- atomicLevel.SetLevel(zapcore.InfoLevel)
- case "WARN":
- atomicLevel.SetLevel(zapcore.WarnLevel)
- case "ERROR":
- atomicLevel.SetLevel(zapcore.ErrorLevel)
- case "DPANIC":
- atomicLevel.SetLevel(zapcore.DPanicLevel)
- case "PANIC":
- atomicLevel.SetLevel(zapcore.PanicLevel)
- case "FATAL":
- atomicLevel.SetLevel(zapcore.FatalLevel)
- default:
- atomicLevel.SetLevel(zapcore.DebugLevel)
- }
- return atomicLevel
- }
|