From e878294a8e54983e3a5462399fec3a8a259f6882 Mon Sep 17 00:00:00 2001 From: Sotirios Mantziaris Date: Sun, 3 Apr 2016 23:00:23 +0300 Subject: [PATCH] added context to std level logger --- std_level_logger.go | 102 +++++++++++++++++++++++++-------------- std_level_logger_test.go | 47 ++++++++++++++---- 2 files changed, 104 insertions(+), 45 deletions(-) diff --git a/std_level_logger.go b/std_level_logger.go index 878d861..03ab8d3 100644 --- a/std_level_logger.go +++ b/std_level_logger.go @@ -36,11 +36,28 @@ func (l LogLevel) String() string { } } +var logFunc func(LogLevel, string, ...interface{}) +var loglnFunc func(LogLevel, string, ...interface{}) +var logfFunc func(LogLevel, string, string, ...interface{}) + +// StdLevelLogger is a level logger based on the standard log package +type StdLevelLogger struct { + logFunc func(LogLevel, string, ...interface{}) + loglnFunc func(LogLevel, string, ...interface{}) + logfFunc func(LogLevel, string, string, ...interface{}) + context string +} + +// NewStdLevelLogger creates a new standard level logger +func NewStdLevelLogger(context string) *StdLevelLogger { + return &StdLevelLogger{logFunc, loglnFunc, logfFunc, context} +} + // ConfigureStdLevelLogger configures the standard level logger with the default log level. // By providing a different io.Writer and prefix you can control the logging output (testing etc) -func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) { +func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer, context string) { - logFunc := func(level LogLevel, args ...interface{}) { + logFunc = func(level LogLevel, context string, args ...interface{}) { if defaultLoglevel > level { return } @@ -48,11 +65,17 @@ func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) { if w != nil { log.SetOutput(w) } - args = prepend(args, strings.Join([]string{level.String(), " "}, "")) + + if context == "" { + args = prepend(args, strings.Join([]string{level.String(), " "}, "")) + } else { + args = prepend(args, level.String(), " ", context, " ") + } + log.Print(args...) } - loglnFunc := func(level LogLevel, args ...interface{}) { + loglnFunc = func(level LogLevel, context string, args ...interface{}) { if defaultLoglevel > level { return } @@ -60,11 +83,17 @@ func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) { if w != nil { log.SetOutput(w) } - args = prepend(args, level.String()) + + if context == "" { + args = prepend(args, level.String()) + } else { + args = prepend(args, level.String(), context) + } + log.Println(args...) } - logfFunc := func(level LogLevel, msg string, args ...interface{}) { + logfFunc = func(level LogLevel, context string, msg string, args ...interface{}) { if defaultLoglevel > level { return } @@ -73,114 +102,117 @@ func ConfigureStdLevelLogger(defaultLoglevel LogLevel, w io.Writer) { log.SetOutput(w) } - log.Printf(strings.Join([]string{level.String(), msg}, " "), args...) + if context == "" { + log.Printf(strings.Join([]string{level.String(), msg}, " "), args...) + } else { + log.Printf(strings.Join([]string{level.String(), context, msg}, " "), args...) + } } - Level = &StdLevelLogger{logFunc, loglnFunc, logfFunc} + Level = NewStdLevelLogger(context) } -func prepend(args []interface{}, value string) []interface{} { - argsExt := make([]interface{}, len(args)+1) - argsExt[0] = value - for index := 0; index < len(args); index++ { - argsExt[index+1] = args[index] +func prepend(args []interface{}, values ...string) []interface{} { + valuesLength := len(values) + argsLength := len(args) + argsExt := make([]interface{}, argsLength+valuesLength) + + for index := 0; index < valuesLength; index++ { + argsExt[index] = values[index] } - return argsExt -} -// StdLevelLogger is a level logger based on the standard log package -type StdLevelLogger struct { - logFunc func(LogLevel, ...interface{}) - loglnFunc func(LogLevel, ...interface{}) - logfFunc func(LogLevel, string, ...interface{}) + for index := valuesLength; index < argsLength+valuesLength; index++ { + argsExt[index] = args[index-valuesLength] + } + return argsExt } // Fatal logging func (l *StdLevelLogger) Fatal(args ...interface{}) { - l.logFunc(FatalLevel, args...) + l.logFunc(FatalLevel, l.context, args...) } // Fatalf logging func (l *StdLevelLogger) Fatalf(msg string, args ...interface{}) { - l.logfFunc(FatalLevel, msg, args...) + l.logfFunc(FatalLevel, l.context, msg, args...) } // Fatalln logging func (l *StdLevelLogger) Fatalln(args ...interface{}) { - l.loglnFunc(FatalLevel, args...) + l.loglnFunc(FatalLevel, l.context, args...) } // Error logging func (l *StdLevelLogger) Error(args ...interface{}) { - l.logFunc(ErrorLevel, args...) + l.logFunc(ErrorLevel, l.context, args...) } // Errorf logging func (l *StdLevelLogger) Errorf(msg string, args ...interface{}) { - l.logfFunc(ErrorLevel, msg, args...) + l.logfFunc(ErrorLevel, l.context, msg, args...) } // Errorln logging func (l *StdLevelLogger) Errorln(args ...interface{}) { - l.loglnFunc(ErrorLevel, args...) + l.loglnFunc(ErrorLevel, l.context, args...) } // Warn logging func (l *StdLevelLogger) Warn(args ...interface{}) { - l.logFunc(WarnLevel, args...) + l.logFunc(WarnLevel, l.context, args...) } // Warnf logging func (l *StdLevelLogger) Warnf(msg string, args ...interface{}) { - l.logfFunc(WarnLevel, msg, args...) + l.logfFunc(WarnLevel, l.context, msg, args...) } // Warnln logging func (l *StdLevelLogger) Warnln(args ...interface{}) { - l.loglnFunc(WarnLevel, args...) + l.loglnFunc(WarnLevel, l.context, args...) } // Info logging func (l *StdLevelLogger) Info(args ...interface{}) { - l.logFunc(InfoLevel, args...) + l.logFunc(InfoLevel, l.context, args...) } // Infof logging func (l *StdLevelLogger) Infof(msg string, args ...interface{}) { - l.logfFunc(InfoLevel, msg, args...) + l.logfFunc(InfoLevel, l.context, msg, args...) } // Infoln logging func (l *StdLevelLogger) Infoln(args ...interface{}) { - l.loglnFunc(InfoLevel, args...) + l.loglnFunc(InfoLevel, l.context, args...) } // Debug logging func (l *StdLevelLogger) Debug(args ...interface{}) { - l.logFunc(DebugLevel, args...) + l.logFunc(DebugLevel, l.context, args...) } // Debugf logging func (l *StdLevelLogger) Debugf(msg string, args ...interface{}) { - l.logfFunc(DebugLevel, msg, args...) + l.logfFunc(DebugLevel, l.context, msg, args...) } // Debugln logging func (l *StdLevelLogger) Debugln(args ...interface{}) { - l.loglnFunc(DebugLevel, args...) + l.loglnFunc(DebugLevel, l.context, args...) } diff --git a/std_level_logger_test.go b/std_level_logger_test.go index d3ed218..dcbb2cd 100644 --- a/std_level_logger_test.go +++ b/std_level_logger_test.go @@ -29,7 +29,7 @@ func TestLogLevelToString(t *testing.T) { func TestStdLevelLoggerNew(t *testing.T) { - ConfigureStdLevelLogger(DebugLevel, nil) + ConfigureStdLevelLogger(DebugLevel, nil, "") if Level == nil { t.Fatal("Should have returned a error") @@ -38,7 +38,7 @@ func TestStdLevelLoggerNew(t *testing.T) { func TestStdLevelLoggerNewWithWriter(t *testing.T) { - ConfigureStdLevelLogger(DebugLevel, new(TestWriter)) + ConfigureStdLevelLogger(DebugLevel, new(TestWriter), "") if Level == nil { t.Fatal("Should have returned a error") @@ -49,7 +49,7 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) { w := new(TestWriter) - ConfigureStdLevelLogger(ErrorLevel, w) + ConfigureStdLevelLogger(ErrorLevel, w, "") Level.Warn("Test") Level.Error("Test") @@ -58,11 +58,11 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) { if len(w.data) != 2 { t.Fatalf("Data should have been 2 %s", w.data) } - if strings.HasSuffix(w.data[0], "Error Test") { + if !strings.HasSuffix(w.data[0], "Error Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } - if strings.HasSuffix(w.data[1], "Fatal Test") { + if !strings.HasSuffix(w.data[1], "Fatal Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } w.data = nil @@ -74,11 +74,11 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) { if len(w.data) != 2 { t.Fatalf("Data should have been 2 %s", w.data) } - if strings.HasSuffix(w.data[0], "Error Test") { + if !strings.HasSuffix(w.data[0], "Error Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } - if strings.HasSuffix(w.data[1], "Fatal Test") { + if !strings.HasSuffix(w.data[1], "Fatal Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } w.data = nil @@ -90,15 +90,42 @@ func TestStdLevelLoggerWithHigherDefaultLevel(t *testing.T) { if len(w.data) != 2 { t.Fatalf("Data should have been 2 %s", w.data) } - if strings.HasSuffix(w.data[0], "Error Test") { + if !strings.HasSuffix(w.data[0], "Error Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } - if strings.HasSuffix(w.data[1], "Fatal Test") { + if !strings.HasSuffix(w.data[1], "Fatal Test\n") { t.Fatalf("Expected %s actual %s", "Error Test", w.data[0]) } } +func TestStdLevelLoggerWithContext(t *testing.T) { + + w := new(TestWriter) + + ConfigureStdLevelLogger(DebugLevel, w, "Context") + + Level.Debug("Test") + Level.Debugf("Test %s", "one") + Level.Debugln("Test") + + if len(w.data) != 3 { + t.Fatalf("Data should have been 3 %s", w.data) + } + + if !strings.HasSuffix(w.data[0], "Debug Context Test\n") { + t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[0]) + } + + if !strings.HasSuffix(w.data[1], "Debug Context Test one\n") { + t.Fatalf("Expected %s actual %s", "Debug Context Test one", w.data[1]) + } + + if !strings.HasSuffix(w.data[2], "Debug Context Test\n") { + t.Fatalf("Expected %s actual %s", "Debug Context Test", w.data[2]) + } +} + var stdLevelLogTests = []struct { in contextLevelLogTestCase out string @@ -127,7 +154,7 @@ var stdLevelLogTests = []struct { func TestStdLeveledLogger(t *testing.T) { w := new(TestWriter) - ConfigureStdLevelLogger(DebugLevel, w) + ConfigureStdLevelLogger(DebugLevel, w, "") logFunctions := make(map[string]logFunction, 0) logFunctions["Debug"] = func(msg string, args ...string) { Level.Debug(convertToInterfaceSlice(args)...) }