github.com/adevinta/lava@v0.7.2/internal/engine/agentlogger_test.go (about) 1 // Copyright 2023 Adevinta 2 3 package engine 4 5 import ( 6 "bytes" 7 "fmt" 8 "log/slog" 9 "runtime" 10 "strings" 11 "testing" 12 ) 13 14 func TestAgentLogger(t *testing.T) { 15 tests := []struct { 16 name string 17 level slog.Level 18 logf func(l agentLogger, format string, args ...any) 19 format string 20 args []any 21 want string 22 }{ 23 { 24 name: "Debugf at debug level", 25 level: slog.LevelDebug, 26 logf: agentLogger.Debugf, 27 format: "msg %v %v", 28 args: []any{"Go", 60}, 29 want: `level=DEBUG source=$SOURCE msg="msg Go 60"`, 30 }, 31 { 32 name: "Debugf at info level", 33 level: slog.LevelInfo, 34 logf: agentLogger.Debugf, 35 format: "msg %v %v", 36 args: []any{"Go", 60}, 37 want: ``, 38 }, 39 { 40 name: "Debugf at warn level", 41 level: slog.LevelWarn, 42 logf: agentLogger.Debugf, 43 format: "msg %v %v", 44 args: []any{"Go", 60}, 45 want: ``, 46 }, 47 { 48 name: "Debugf at error level", 49 level: slog.LevelError, 50 logf: agentLogger.Debugf, 51 format: "msg %v %v", 52 args: []any{"Go", 60}, 53 want: ``, 54 }, 55 { 56 name: "Infof at debug level", 57 level: slog.LevelDebug, 58 logf: agentLogger.Infof, 59 format: "msg %v %v", 60 args: []any{"Go", 60}, 61 want: `level=INFO source=$SOURCE msg="msg Go 60"`, 62 }, 63 { 64 name: "Infof at info level", 65 level: slog.LevelInfo, 66 logf: agentLogger.Infof, 67 format: "msg %v %v", 68 args: []any{"Go", 60}, 69 want: `level=INFO source=$SOURCE msg="msg Go 60"`, 70 }, 71 { 72 name: "Infof at warn level", 73 level: slog.LevelWarn, 74 logf: agentLogger.Infof, 75 format: "msg %v %v", 76 args: []any{"Go", 60}, 77 want: ``, 78 }, 79 { 80 name: "Infof at error level", 81 level: slog.LevelError, 82 logf: agentLogger.Infof, 83 format: "msg %v %v", 84 args: []any{"Go", 60}, 85 want: ``, 86 }, 87 { 88 name: "Errorf at debug level", 89 level: slog.LevelDebug, 90 logf: agentLogger.Errorf, 91 format: "msg %v %v", 92 args: []any{"Go", 60}, 93 want: `level=ERROR source=$SOURCE msg="msg Go 60"`, 94 }, 95 { 96 name: "Errorf at info level", 97 level: slog.LevelInfo, 98 logf: agentLogger.Errorf, 99 format: "msg %v %v", 100 args: []any{"Go", 60}, 101 want: `level=ERROR source=$SOURCE msg="msg Go 60"`, 102 }, 103 { 104 name: "Errorf at warn level", 105 level: slog.LevelWarn, 106 logf: agentLogger.Errorf, 107 format: "msg %v %v", 108 args: []any{"Go", 60}, 109 want: `level=ERROR source=$SOURCE msg="msg Go 60"`, 110 }, 111 { 112 name: "Errorf at error level", 113 level: slog.LevelError, 114 logf: agentLogger.Errorf, 115 format: "msg %v %v", 116 args: []any{"Go", 60}, 117 want: `level=ERROR source=$SOURCE msg="msg Go 60"`, 118 }, 119 } 120 121 for _, tt := range tests { 122 t.Run(tt.name, func(t *testing.T) { 123 var buf bytes.Buffer 124 handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{ 125 Level: tt.level, 126 AddSource: true, 127 ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { 128 // Remove time. 129 if a.Key == slog.TimeKey { 130 return slog.Attr{} 131 } 132 return a 133 }, 134 }) 135 logger := newAgentLogger(slog.New(handler)) 136 137 tt.logf(logger, tt.format, tt.args...) 138 _, file, line, ok := runtime.Caller(0) 139 if !ok { 140 t.Fatalf("could not get source line") 141 } 142 143 // The call to tt.logf happens one line before 144 // calling runtime.Caller. 145 source := fmt.Sprintf("%v:%v", file, line-1) 146 147 got := strings.TrimSuffix(buf.String(), "\n") 148 want := strings.ReplaceAll(tt.want, "$SOURCE", source) 149 if got != want { 150 t.Errorf("unexpected output: got: %#q, want: %#q", got, want) 151 } 152 }) 153 } 154 }