github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/mlog/stdlog.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  	"strings"
     8  
     9  	"go.uber.org/zap"
    10  	"go.uber.org/zap/zapcore"
    11  )
    12  
    13  // Implementation of zapcore.Core to interpret log messages from a standard logger
    14  // and translate the levels to zapcore levels.
    15  type stdLogLevelInterpreterCore struct {
    16  	wrappedCore zapcore.Core
    17  }
    18  
    19  func stdLogInterpretZapEntry(entry zapcore.Entry) zapcore.Entry {
    20  	message := entry.Message
    21  	if strings.Index(message, "[DEBUG]") == 0 {
    22  		entry.Level = zapcore.DebugLevel
    23  		entry.Message = message[7:]
    24  	} else if strings.Index(message, "[DEBG]") == 0 {
    25  		entry.Level = zapcore.DebugLevel
    26  		entry.Message = message[6:]
    27  	} else if strings.Index(message, "[WARN]") == 0 {
    28  		entry.Level = zapcore.WarnLevel
    29  		entry.Message = message[6:]
    30  	} else if strings.Index(message, "[ERROR]") == 0 {
    31  		entry.Level = zapcore.ErrorLevel
    32  		entry.Message = message[7:]
    33  	} else if strings.Index(message, "[EROR]") == 0 {
    34  		entry.Level = zapcore.ErrorLevel
    35  		entry.Message = message[6:]
    36  	} else if strings.Index(message, "[ERR]") == 0 {
    37  		entry.Level = zapcore.ErrorLevel
    38  		entry.Message = message[5:]
    39  	} else if strings.Index(message, "[INFO]") == 0 {
    40  		entry.Level = zapcore.InfoLevel
    41  		entry.Message = message[6:]
    42  	}
    43  	return entry
    44  }
    45  
    46  func (s *stdLogLevelInterpreterCore) Enabled(lvl zapcore.Level) bool {
    47  	return s.wrappedCore.Enabled(lvl)
    48  }
    49  
    50  func (s *stdLogLevelInterpreterCore) With(fields []zapcore.Field) zapcore.Core {
    51  	return s.wrappedCore.With(fields)
    52  }
    53  
    54  func (s *stdLogLevelInterpreterCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry {
    55  	entry = stdLogInterpretZapEntry(entry)
    56  	return s.wrappedCore.Check(entry, checkedEntry)
    57  }
    58  
    59  func (s *stdLogLevelInterpreterCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
    60  	entry = stdLogInterpretZapEntry(entry)
    61  	return s.wrappedCore.Write(entry, fields)
    62  }
    63  
    64  func (s *stdLogLevelInterpreterCore) Sync() error {
    65  	return s.wrappedCore.Sync()
    66  }
    67  
    68  func getStdLogOption() zap.Option {
    69  	return zap.WrapCore(
    70  		func(core zapcore.Core) zapcore.Core {
    71  			return &stdLogLevelInterpreterCore{core}
    72  		},
    73  	)
    74  }