summaryrefslogtreecommitdiffstats
path: root/mlog
diff options
context:
space:
mode:
Diffstat (limited to 'mlog')
-rw-r--r--mlog/global.go6
-rw-r--r--mlog/log.go29
-rw-r--r--mlog/stdlog.go13
-rw-r--r--mlog/sugar.go28
4 files changed, 73 insertions, 3 deletions
diff --git a/mlog/global.go b/mlog/global.go
index 36dd51afb..ba90ace2f 100644
--- a/mlog/global.go
+++ b/mlog/global.go
@@ -11,7 +11,9 @@ import (
var globalLogger *Logger
func InitGlobalLogger(logger *Logger) {
- globalLogger = logger
+ glob := *logger
+ glob.zap = glob.zap.WithOptions(zap.AddCallerSkip(1))
+ globalLogger = &glob
Debug = globalLogger.Debug
Info = globalLogger.Info
Warn = globalLogger.Warn
@@ -20,7 +22,7 @@ func InitGlobalLogger(logger *Logger) {
}
func RedirectStdLog(logger *Logger) {
- zap.RedirectStdLogAt(logger.zap.With(zap.String("source", "stdlog")), zapcore.ErrorLevel)
+ zap.RedirectStdLogAt(logger.zap.With(zap.String("source", "stdlog")).WithOptions(zap.AddCallerSkip(-2)), zapcore.ErrorLevel)
}
type LogFunc func(string, ...Field)
diff --git a/mlog/log.go b/mlog/log.go
index c3261459b..e3bc38d83 100644
--- a/mlog/log.go
+++ b/mlog/log.go
@@ -4,6 +4,7 @@
package mlog
import (
+ "io"
"log"
"os"
@@ -28,9 +29,11 @@ type Field = zapcore.Field
var Int64 = zap.Int64
var Int = zap.Int
+var Uint32 = zap.Uint32
var String = zap.String
var Any = zap.Any
var Err = zap.Error
+var Bool = zap.Bool
type LoggerConfiguration struct {
EnableConsole bool
@@ -99,7 +102,7 @@ func NewLogger(config *LoggerConfiguration) *Logger {
combinedCore := zapcore.NewTee(cores...)
logger.zap = zap.New(combinedCore,
- zap.AddCallerSkip(2),
+ zap.AddCallerSkip(1),
zap.AddCaller(),
)
@@ -125,6 +128,30 @@ func (l *Logger) StdLog(fields ...Field) *log.Logger {
return zap.NewStdLog(l.With(fields...).zap.WithOptions(getStdLogOption()))
}
+// StdLogWriter returns a writer that can be hooked up to the output of a golang standard logger
+// anything written will be interpreted as log entries accordingly
+func (l *Logger) StdLogWriter() io.Writer {
+ newLogger := *l
+ newLogger.zap = newLogger.zap.WithOptions(zap.AddCallerSkip(4), getStdLogOption())
+ f := newLogger.Info
+ return &loggerWriter{f}
+}
+
+func (l *Logger) WithCallerSkip(skip int) *Logger {
+ newlogger := *l
+ newlogger.zap = newlogger.zap.WithOptions(zap.AddCallerSkip(skip))
+ return &newlogger
+}
+
+// Made for the plugin interface, wraps mlog in a simpler interface
+// at the cost of performance
+func (l *Logger) Sugar() *SugarLogger {
+ return &SugarLogger{
+ wrappedLogger: l,
+ zapSugar: l.zap.Sugar(),
+ }
+}
+
func (l *Logger) Debug(message string, fields ...Field) {
l.zap.Debug(message, fields...)
}
diff --git a/mlog/stdlog.go b/mlog/stdlog.go
index a875b2e32..7839ddfa0 100644
--- a/mlog/stdlog.go
+++ b/mlog/stdlog.go
@@ -4,6 +4,7 @@
package mlog
import (
+ "bytes"
"strings"
"go.uber.org/zap"
@@ -72,3 +73,15 @@ func getStdLogOption() zap.Option {
},
)
}
+
+type loggerWriter struct {
+ logFunc func(msg string, fields ...Field)
+}
+
+func (l *loggerWriter) Write(p []byte) (int, error) {
+ trimmed := string(bytes.TrimSpace(p))
+ for _, line := range strings.Split(trimmed, "\n") {
+ l.logFunc(string(line))
+ }
+ return len(p), nil
+}
diff --git a/mlog/sugar.go b/mlog/sugar.go
new file mode 100644
index 000000000..c00a8bbfc
--- /dev/null
+++ b/mlog/sugar.go
@@ -0,0 +1,28 @@
+// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
+// See LICENSE.txt for license information.
+
+package mlog
+
+import "go.uber.org/zap"
+
+// Made for the plugin interface, use the regular logger for other uses
+type SugarLogger struct {
+ wrappedLogger *Logger
+ zapSugar *zap.SugaredLogger
+}
+
+func (l *SugarLogger) Debug(msg string, keyValuePairs ...interface{}) {
+ l.zapSugar.Debugw(msg, keyValuePairs...)
+}
+
+func (l *SugarLogger) Info(msg string, keyValuePairs ...interface{}) {
+ l.zapSugar.Infow(msg, keyValuePairs...)
+}
+
+func (l *SugarLogger) Error(msg string, keyValuePairs ...interface{}) {
+ l.zapSugar.Errorw(msg, keyValuePairs...)
+}
+
+func (l *SugarLogger) Warn(msg string, keyValuePairs ...interface{}) {
+ l.zapSugar.Warnw(msg, keyValuePairs...)
+}