@@ -80,40 +80,67 @@ func Make(sinks ...Sink) Logger {
80
80
}
81
81
82
82
// Debug logs the msg and fields at LevelDebug.
83
- func (l Logger ) Debug (ctx context.Context , msg string , fields ... Field ) {
83
+ // See Info for information on the fields argument.
84
+ func (l Logger ) Debug (ctx context.Context , msg string , fields ... any ) {
84
85
l .log (ctx , LevelDebug , msg , fields )
85
86
}
86
87
87
88
// Info logs the msg and fields at LevelInfo.
88
- func (l Logger ) Info (ctx context.Context , msg string , fields ... Field ) {
89
+ // Fields may contain any combination of key value pairs, Field, and Map.
90
+ // For example:
91
+ //
92
+ // log.Info(ctx, "something happened", "user", "alex", slog.F("age", 20))
93
+ //
94
+ // is equivalent to:
95
+ //
96
+ // log.Info(ctx, "something happened", slog.F("user", "alex"), slog.F("age", 20))
97
+ //
98
+ // is equivalent to:
99
+ //
100
+ // log.Info(ctx, "something happened", slog.M(
101
+ // slog.F("user", "alex"),
102
+ // slog.F("age", 20),
103
+ // ))
104
+ //
105
+ // is equivalent to:
106
+ //
107
+ // log.Info(ctx, "something happened", "user", "alex", "age", 20)
108
+ //
109
+ // In general, prefer using key value pairs over Field and Map, as that is how
110
+ // the standard library's slog package works.
111
+ func (l Logger ) Info (ctx context.Context , msg string , fields ... any ) {
89
112
l .log (ctx , LevelInfo , msg , fields )
90
113
}
91
114
92
115
// Warn logs the msg and fields at LevelWarn.
93
- func (l Logger ) Warn (ctx context.Context , msg string , fields ... Field ) {
116
+ // See Info() for information on the fields argument.
117
+ func (l Logger ) Warn (ctx context.Context , msg string , fields ... any ) {
94
118
l .log (ctx , LevelWarn , msg , fields )
95
119
}
96
120
97
121
// Error logs the msg and fields at LevelError.
122
+ // See Info() for information on the fields argument.
98
123
//
99
124
// It will then Sync().
100
- func (l Logger ) Error (ctx context.Context , msg string , fields ... Field ) {
125
+ func (l Logger ) Error (ctx context.Context , msg string , fields ... any ) {
101
126
l .log (ctx , LevelError , msg , fields )
102
127
l .Sync ()
103
128
}
104
129
105
130
// Critical logs the msg and fields at LevelCritical.
131
+ // See Info() for information on the fields argument.
106
132
//
107
133
// It will then Sync().
108
- func (l Logger ) Critical (ctx context.Context , msg string , fields ... Field ) {
134
+ func (l Logger ) Critical (ctx context.Context , msg string , fields ... any ) {
109
135
l .log (ctx , LevelCritical , msg , fields )
110
136
l .Sync ()
111
137
}
112
138
113
139
// Fatal logs the msg and fields at LevelFatal.
140
+ // See Info() for information on the fields argument.
114
141
//
115
142
// It will then Sync() and os.Exit(1).
116
- func (l Logger ) Fatal (ctx context.Context , msg string , fields ... Field ) {
143
+ func (l Logger ) Fatal (ctx context.Context , msg string , fields ... any ) {
117
144
l .log (ctx , LevelFatal , msg , fields )
118
145
l .Sync ()
119
146
@@ -155,7 +182,32 @@ func (l Logger) AppendSinks(s ...Sink) Logger {
155
182
return l
156
183
}
157
184
158
- func (l Logger ) log (ctx context.Context , level Level , msg string , fields Map ) {
185
+ func (l Logger ) log (ctx context.Context , level Level , msg string , rawFields []any ) {
186
+ fields := make (Map , 0 , len (rawFields ))
187
+ var wipField Field
188
+ for i , f := range rawFields {
189
+ if wipField .Name != "" {
190
+ wipField .Value = f
191
+ fields = append (fields , wipField )
192
+ wipField = Field {}
193
+ continue
194
+ }
195
+ switch f := f .(type ) {
196
+ case Field :
197
+ fields = append (fields , f )
198
+ case Map :
199
+ fields = append (fields , f ... )
200
+ case string :
201
+ wipField .Name = f
202
+ default :
203
+ panic (fmt .Sprintf ("unexpected field type %T at index %v (does it have a key?)" , f , i ))
204
+ }
205
+ }
206
+
207
+ if wipField .Name != "" {
208
+ panic (fmt .Sprintf ("field %q has no value" , wipField .Name ))
209
+ }
210
+
159
211
ent := l .entry (ctx , level , msg , fields )
160
212
l .Log (ctx , ent )
161
213
}
0 commit comments