github.com/keys-pub/mattermost-server@v4.10.10+incompatible/mlog/log.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package mlog 5 6 import ( 7 "log" 8 "os" 9 10 "go.uber.org/zap" 11 "go.uber.org/zap/zapcore" 12 "gopkg.in/natefinch/lumberjack.v2" 13 ) 14 15 const ( 16 // Very verbose messages for debugging specific issues 17 LevelDebug = "debug" 18 // Default log level, informational 19 LevelInfo = "info" 20 // Warnings are messages about possible issues 21 LevelWarn = "warn" 22 // Errors are messages about things we know are problems 23 LevelError = "error" 24 ) 25 26 // Type and function aliases from zap to limit the libraries scope into MM code 27 type Field = zapcore.Field 28 29 var Int64 = zap.Int64 30 var Int = zap.Int 31 var String = zap.String 32 var Err = zap.Error 33 34 type LoggerConfiguration struct { 35 EnableConsole bool 36 ConsoleJson bool 37 ConsoleLevel string 38 EnableFile bool 39 FileJson bool 40 FileLevel string 41 FileLocation string 42 } 43 44 type Logger struct { 45 zap *zap.Logger 46 consoleLevel zap.AtomicLevel 47 fileLevel zap.AtomicLevel 48 } 49 50 func getZapLevel(level string) zapcore.Level { 51 switch level { 52 case LevelInfo: 53 return zapcore.InfoLevel 54 case LevelWarn: 55 return zapcore.WarnLevel 56 case LevelDebug: 57 return zapcore.DebugLevel 58 case LevelError: 59 return zapcore.ErrorLevel 60 default: 61 return zapcore.InfoLevel 62 } 63 } 64 65 func makeEncoder(json bool) zapcore.Encoder { 66 encoderConfig := zap.NewProductionEncoderConfig() 67 if json { 68 return zapcore.NewJSONEncoder(encoderConfig) 69 } 70 71 encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder 72 return zapcore.NewConsoleEncoder(encoderConfig) 73 } 74 75 func NewLogger(config *LoggerConfiguration) *Logger { 76 cores := []zapcore.Core{} 77 logger := &Logger{ 78 consoleLevel: zap.NewAtomicLevelAt(getZapLevel(config.ConsoleLevel)), 79 fileLevel: zap.NewAtomicLevelAt(getZapLevel(config.FileLevel)), 80 } 81 82 if config.EnableConsole { 83 writer := zapcore.Lock(os.Stdout) 84 core := zapcore.NewCore(makeEncoder(config.ConsoleJson), writer, logger.consoleLevel) 85 cores = append(cores, core) 86 } 87 88 if config.EnableFile { 89 writer := zapcore.AddSync(&lumberjack.Logger{ 90 Filename: config.FileLocation, 91 MaxSize: 100, 92 Compress: true, 93 }) 94 core := zapcore.NewCore(makeEncoder(config.FileJson), writer, logger.fileLevel) 95 cores = append(cores, core) 96 } 97 98 combinedCore := zapcore.NewTee(cores...) 99 100 logger.zap = zap.New(combinedCore, 101 zap.AddCallerSkip(2), 102 zap.AddCaller(), 103 ) 104 105 return logger 106 } 107 108 func (l *Logger) ChangeLevels(config *LoggerConfiguration) { 109 l.consoleLevel.SetLevel(getZapLevel(config.ConsoleLevel)) 110 l.fileLevel.SetLevel(getZapLevel(config.FileLevel)) 111 } 112 113 func (l *Logger) SetConsoleLevel(level string) { 114 l.consoleLevel.SetLevel(getZapLevel(level)) 115 } 116 117 func (l *Logger) With(fields ...Field) *Logger { 118 newlogger := *l 119 newlogger.zap = newlogger.zap.With(fields...) 120 return &newlogger 121 } 122 123 func (l *Logger) StdLog(fields ...Field) *log.Logger { 124 return zap.NewStdLog(l.With(fields...).zap.WithOptions(getStdLogOption())) 125 } 126 127 func (l *Logger) Debug(message string, fields ...Field) { 128 l.zap.Debug(message, fields...) 129 } 130 131 func (l *Logger) Info(message string, fields ...Field) { 132 l.zap.Info(message, fields...) 133 } 134 135 func (l *Logger) Warn(message string, fields ...Field) { 136 l.zap.Warn(message, fields...) 137 } 138 139 func (l *Logger) Error(message string, fields ...Field) { 140 l.zap.Error(message, fields...) 141 } 142 143 func (l *Logger) Critical(message string, fields ...Field) { 144 l.zap.Error(message, fields...) 145 }