diff --git a/log/default_test.go b/log/default_test.go index a0cb281497..0f57d2009f 100644 --- a/log/default_test.go +++ b/log/default_test.go @@ -182,7 +182,132 @@ func Test_LogfKeyAndValues(t *testing.T) { } } -func BenchmarkLogfKeyAndValues(b *testing.B) { +func Test_SetLevel(t *testing.T) { + setLogger := &defaultLogger{ + stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), + depth: 4, + } + + setLogger.SetLevel(LevelTrace) + require.Equal(t, LevelTrace, setLogger.level) + require.Equal(t, LevelTrace.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelDebug) + require.Equal(t, LevelDebug, setLogger.level) + require.Equal(t, LevelDebug.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelInfo) + require.Equal(t, LevelInfo, setLogger.level) + require.Equal(t, LevelInfo.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelWarn) + require.Equal(t, LevelWarn, setLogger.level) + require.Equal(t, LevelWarn.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelError) + require.Equal(t, LevelError, setLogger.level) + require.Equal(t, LevelError.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelFatal) + require.Equal(t, LevelFatal, setLogger.level) + require.Equal(t, LevelFatal.toString(), setLogger.level.toString()) + + setLogger.SetLevel(LevelPanic) + require.Equal(t, LevelPanic, setLogger.level) + require.Equal(t, LevelPanic.toString(), setLogger.level.toString()) + + setLogger.SetLevel(8) + require.Equal(t, 8, int(setLogger.level)) + require.Equal(t, "[?8] ", setLogger.level.toString()) +} + +func Test_Debugw(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "debug work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + Debugw(msg, keysAndValues...) + + require.Equal(t, "[Debug] debug work key1=value1 key2=value2\n", string(w.b)) +} + +func Test_Infow(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "info work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + Infow(msg, keysAndValues...) + + require.Equal(t, "[Info] info work key1=value1 key2=value2\n", string(w.b)) +} + +func Test_Warnw(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "warning work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + Warnw(msg, keysAndValues...) + + require.Equal(t, "[Warn] warning work key1=value1 key2=value2\n", string(w.b)) +} + +func Test_Errorw(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "error work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + Errorw(msg, keysAndValues...) + + require.Equal(t, "[Error] error work key1=value1 key2=value2\n", string(w.b)) +} + +func Test_Panicw(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "panic work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + require.Panics(t, func() { + Panicw(msg, keysAndValues...) + }) + + require.Equal(t, "[Panic] panic work key1=value1 key2=value2\n", string(w.b)) +} + +func Test_Tracew(t *testing.T) { + initDefaultLogger() + + var w byteSliceWriter + SetOutput(&w) + + msg := "trace work" + keysAndValues := []any{"key1", "value1", "key2", "value2"} + + Tracew(msg, keysAndValues...) + + require.Equal(t, "[Trace] trace work key1=value1 key2=value2\n", string(w.b)) +} + +func Benchmark_LogfKeyAndValues(b *testing.B) { tests := []struct { name string level Level @@ -222,55 +347,78 @@ func BenchmarkLogfKeyAndValues(b *testing.B) { } for _, tt := range tests { - b.Run(tt.name, func(b *testing.B) { + b.Run(tt.name, func(bb *testing.B) { var buf bytes.Buffer l := &defaultLogger{ stdlog: log.New(&buf, "", 0), level: tt.level, depth: 4, } - for i := 0; i < b.N; i++ { + + bb.ReportAllocs() + bb.ResetTimer() + + for i := 0; i < bb.N; i++ { l.privateLogw(tt.level, tt.format, tt.keysAndValues) } }) } } -func Test_SetLevel(t *testing.T) { - setLogger := &defaultLogger{ - stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), - depth: 4, +func Benchmark_LogfKeyAndValuesParallel(b *testing.B) { + tests := []struct { + name string + level Level + format string + keysAndValues []any + }{ + { + name: "debug level with key-values", + level: LevelDebug, + format: "", + keysAndValues: []any{"name", "Bob", "age", 30}, + }, + { + name: "info level with key-values", + level: LevelInfo, + format: "", + keysAndValues: []any{"status", "ok", "code", 200}, + }, + { + name: "warn level with key-values", + level: LevelWarn, + format: "", + keysAndValues: []any{"error", "not found", "id", 123}, + }, + { + name: "warn level with format and key-values", + level: LevelWarn, + format: "test", + keysAndValues: []any{"error", "not found", "id", 123}, + }, + { + name: "warn level with one key", + level: LevelWarn, + format: "", + keysAndValues: []any{"error"}, + }, } - setLogger.SetLevel(LevelTrace) - require.Equal(t, LevelTrace, setLogger.level) - require.Equal(t, LevelTrace.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelDebug) - require.Equal(t, LevelDebug, setLogger.level) - require.Equal(t, LevelDebug.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelInfo) - require.Equal(t, LevelInfo, setLogger.level) - require.Equal(t, LevelInfo.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelWarn) - require.Equal(t, LevelWarn, setLogger.level) - require.Equal(t, LevelWarn.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelError) - require.Equal(t, LevelError, setLogger.level) - require.Equal(t, LevelError.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelFatal) - require.Equal(t, LevelFatal, setLogger.level) - require.Equal(t, LevelFatal.toString(), setLogger.level.toString()) - - setLogger.SetLevel(LevelPanic) - require.Equal(t, LevelPanic, setLogger.level) - require.Equal(t, LevelPanic.toString(), setLogger.level.toString()) - - setLogger.SetLevel(8) - require.Equal(t, 8, int(setLogger.level)) - require.Equal(t, "[?8] ", setLogger.level.toString()) + for _, tt := range tests { + b.Run(tt.name, func(bb *testing.B) { + bb.ReportAllocs() + bb.ResetTimer() + bb.RunParallel(func(pb *testing.PB) { + var buf bytes.Buffer + l := &defaultLogger{ + stdlog: log.New(&buf, "", 0), + level: tt.level, + depth: 4, + } + for pb.Next() { + l.privateLogw(tt.level, tt.format, tt.keysAndValues) + } + }) + }) + } } diff --git a/log/fiberlog_test.go b/log/fiberlog_test.go index eb7b077a9b..ca6e93c83a 100644 --- a/log/fiberlog_test.go +++ b/log/fiberlog_test.go @@ -22,3 +22,50 @@ func Test_SetLogger(t *testing.T) { SetLogger(setLog) require.Equal(t, logger, setLog) } + +func Test_Fiberlog_SetLevel(t *testing.T) { + // Set up + mockLogger := &defaultLogger{} + SetLogger(mockLogger) + + // Test cases + testCases := []struct { + name string + level Level + expected Level + }{ + { + name: "Test case 1", + level: LevelDebug, + expected: LevelDebug, + }, + { + name: "Test case 2", + level: LevelInfo, + expected: LevelInfo, + }, + { + name: "Test case 3", + level: LevelWarn, + expected: LevelWarn, + }, + { + name: "Test case 4", + level: LevelError, + expected: LevelError, + }, + { + name: "Test case 5", + level: LevelFatal, + expected: LevelFatal, + }, + } + + // Run tests + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + SetLevel(tc.level) + require.Equal(t, tc.expected, mockLogger.level) + }) + } +}