github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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 Any = zap.Any
    33  var Err = zap.Error
    34  
    35  type LoggerConfiguration struct {
    36  	EnableConsole bool
    37  	ConsoleJson   bool
    38  	ConsoleLevel  string
    39  	EnableFile    bool
    40  	FileJson      bool
    41  	FileLevel     string
    42  	FileLocation  string
    43  }
    44  
    45  type Logger struct {
    46  	zap          *zap.Logger
    47  	consoleLevel zap.AtomicLevel
    48  	fileLevel    zap.AtomicLevel
    49  }
    50  
    51  func getZapLevel(level string) zapcore.Level {
    52  	switch level {
    53  	case LevelInfo:
    54  		return zapcore.InfoLevel
    55  	case LevelWarn:
    56  		return zapcore.WarnLevel
    57  	case LevelDebug:
    58  		return zapcore.DebugLevel
    59  	case LevelError:
    60  		return zapcore.ErrorLevel
    61  	default:
    62  		return zapcore.InfoLevel
    63  	}
    64  }
    65  
    66  func makeEncoder(json bool) zapcore.Encoder {
    67  	encoderConfig := zap.NewProductionEncoderConfig()
    68  	if json {
    69  		return zapcore.NewJSONEncoder(encoderConfig)
    70  	}
    71  
    72  	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
    73  	return zapcore.NewConsoleEncoder(encoderConfig)
    74  }
    75  
    76  func NewLogger(config *LoggerConfiguration) *Logger {
    77  	cores := []zapcore.Core{}
    78  	logger := &Logger{
    79  		consoleLevel: zap.NewAtomicLevelAt(getZapLevel(config.ConsoleLevel)),
    80  		fileLevel:    zap.NewAtomicLevelAt(getZapLevel(config.FileLevel)),
    81  	}
    82  
    83  	if config.EnableConsole {
    84  		writer := zapcore.Lock(os.Stdout)
    85  		core := zapcore.NewCore(makeEncoder(config.ConsoleJson), writer, logger.consoleLevel)
    86  		cores = append(cores, core)
    87  	}
    88  
    89  	if config.EnableFile {
    90  		writer := zapcore.AddSync(&lumberjack.Logger{
    91  			Filename: config.FileLocation,
    92  			MaxSize:  100,
    93  			Compress: true,
    94  		})
    95  		core := zapcore.NewCore(makeEncoder(config.FileJson), writer, logger.fileLevel)
    96  		cores = append(cores, core)
    97  	}
    98  
    99  	combinedCore := zapcore.NewTee(cores...)
   100  
   101  	logger.zap = zap.New(combinedCore,
   102  		zap.AddCallerSkip(2),
   103  		zap.AddCaller(),
   104  	)
   105  
   106  	return logger
   107  }
   108  
   109  func (l *Logger) ChangeLevels(config *LoggerConfiguration) {
   110  	l.consoleLevel.SetLevel(getZapLevel(config.ConsoleLevel))
   111  	l.fileLevel.SetLevel(getZapLevel(config.FileLevel))
   112  }
   113  
   114  func (l *Logger) SetConsoleLevel(level string) {
   115  	l.consoleLevel.SetLevel(getZapLevel(level))
   116  }
   117  
   118  func (l *Logger) With(fields ...Field) *Logger {
   119  	newlogger := *l
   120  	newlogger.zap = newlogger.zap.With(fields...)
   121  	return &newlogger
   122  }
   123  
   124  func (l *Logger) StdLog(fields ...Field) *log.Logger {
   125  	return zap.NewStdLog(l.With(fields...).zap.WithOptions(getStdLogOption()))
   126  }
   127  
   128  func (l *Logger) Debug(message string, fields ...Field) {
   129  	l.zap.Debug(message, fields...)
   130  }
   131  
   132  func (l *Logger) Info(message string, fields ...Field) {
   133  	l.zap.Info(message, fields...)
   134  }
   135  
   136  func (l *Logger) Warn(message string, fields ...Field) {
   137  	l.zap.Warn(message, fields...)
   138  }
   139  
   140  func (l *Logger) Error(message string, fields ...Field) {
   141  	l.zap.Error(message, fields...)
   142  }
   143  
   144  func (l *Logger) Critical(message string, fields ...Field) {
   145  	l.zap.Error(message, fields...)
   146  }