diff options
author | Christopher Speller <crspeller@gmail.com> | 2018-04-30 08:18:04 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-30 08:18:04 -0700 |
commit | 9c5815ee41f29e27774d17382d9a4bd10d208545 (patch) | |
tree | 698f38053a5f34e337c6750f95b909028a1649dc /mlog/stdlog.go | |
parent | 2f175001b45fb282600e4a9aee3c8a79fc682821 (diff) | |
download | chat-9c5815ee41f29e27774d17382d9a4bd10d208545.tar.gz chat-9c5815ee41f29e27774d17382d9a4bd10d208545.tar.bz2 chat-9c5815ee41f29e27774d17382d9a4bd10d208545.zip |
Add stdlog interpreter. (#8691)
Diffstat (limited to 'mlog/stdlog.go')
-rw-r--r-- | mlog/stdlog.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/mlog/stdlog.go b/mlog/stdlog.go new file mode 100644 index 000000000..a875b2e32 --- /dev/null +++ b/mlog/stdlog.go @@ -0,0 +1,74 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package mlog + +import ( + "strings" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +// Implementation of zapcore.Core to interpret log messages from a standard logger +// and translate the levels to zapcore levels. +type stdLogLevelInterpreterCore struct { + wrappedCore zapcore.Core +} + +func stdLogInterpretZapEntry(entry zapcore.Entry) zapcore.Entry { + message := entry.Message + if strings.Index(message, "[DEBUG]") == 0 { + entry.Level = zapcore.DebugLevel + entry.Message = message[7:] + } else if strings.Index(message, "[DEBG]") == 0 { + entry.Level = zapcore.DebugLevel + entry.Message = message[6:] + } else if strings.Index(message, "[WARN]") == 0 { + entry.Level = zapcore.WarnLevel + entry.Message = message[6:] + } else if strings.Index(message, "[ERROR]") == 0 { + entry.Level = zapcore.ErrorLevel + entry.Message = message[7:] + } else if strings.Index(message, "[EROR]") == 0 { + entry.Level = zapcore.ErrorLevel + entry.Message = message[6:] + } else if strings.Index(message, "[ERR]") == 0 { + entry.Level = zapcore.ErrorLevel + entry.Message = message[5:] + } else if strings.Index(message, "[INFO]") == 0 { + entry.Level = zapcore.InfoLevel + entry.Message = message[6:] + } + return entry +} + +func (s *stdLogLevelInterpreterCore) Enabled(lvl zapcore.Level) bool { + return s.wrappedCore.Enabled(lvl) +} + +func (s *stdLogLevelInterpreterCore) With(fields []zapcore.Field) zapcore.Core { + return s.wrappedCore.With(fields) +} + +func (s *stdLogLevelInterpreterCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry { + entry = stdLogInterpretZapEntry(entry) + return s.wrappedCore.Check(entry, checkedEntry) +} + +func (s *stdLogLevelInterpreterCore) Write(entry zapcore.Entry, fields []zapcore.Field) error { + entry = stdLogInterpretZapEntry(entry) + return s.wrappedCore.Write(entry, fields) +} + +func (s *stdLogLevelInterpreterCore) Sync() error { + return s.wrappedCore.Sync() +} + +func getStdLogOption() zap.Option { + return zap.WrapCore( + func(core zapcore.Core) zapcore.Core { + return &stdLogLevelInterpreterCore{core} + }, + ) +} |