@@ -132,27 +132,25 @@ func AddTracingContext(span trace.Span, err ...error) []slog.Attr {
132
132
// AddTracingContextWithAttributes lets you add the trace context to a structured log, including attribute.KeyValue's to extend the log
133
133
func AddTracingContextWithAttributes (span trace.Span , attributes []attribute.KeyValue , err ... error ) []slog.Attr {
134
134
var result []slog.Attr
135
- if len (err ) > 0 && err [0 ] != nil {
136
- span .RecordError (err [0 ])
137
- span .SetStatus (codes .Error , err [0 ].Error ())
138
- result = append (result , slog .String ("error" , err [0 ].Error ()))
139
- }
135
+ result = handleError (span , err , result )
136
+ result = addTraceContextToLog (span , result )
137
+ result = addServiceName (result )
140
138
141
- result = append (result ,
142
- slog .String (_traceId , span .SpanContext ().TraceID ().String ()),
143
- slog .String (_spanId , span .SpanContext ().SpanID ().String ()),
144
- )
145
- // set service.name if the value isn't empty
146
- if _serviceName != "" {
147
- result = append (result , slog .String ("service.name" , _serviceName ))
148
- }
139
+ // _attributes are a global set of attributes added at initialization
140
+ attributes = append (attributes , _attributes ... )
149
141
150
- attrs := attributes
151
- attrs = append ( attrs , _attributes ... )
142
+ // add attributes when global or passed in attributes are > 0
143
+ result = ConvertToSlogFormat ( attributes , result )
152
144
153
- // add attributes when global or passed attributes are > 0
154
- if len (attrs ) > 0 {
155
- for _ , attr := range attrs {
145
+ return result
146
+ }
147
+
148
+ // ConvertToSlogFormat converts a list of attribute.KeyValue into the slog.Attr format
149
+ // and appends them to "result". The different types of attribute.KeyValue's
150
+ // are converted accordingly.
151
+ func ConvertToSlogFormat (attributes []attribute.KeyValue , result []slog.Attr ) []slog.Attr {
152
+ if len (attributes ) > 0 {
153
+ for _ , attr := range attributes {
156
154
switch attr .Value .Type () {
157
155
case attribute .STRING :
158
156
result = append (result , slog .String (_attrPrefix + "." + string (attr .Key ), attr .Value .AsString ()))
@@ -198,21 +196,55 @@ func AddTracingContextWithAttributes(span trace.Span, attributes []attribute.Key
198
196
}
199
197
}
200
198
}
199
+ return result
200
+ }
201
+
202
+ func addServiceName (result []slog.Attr ) []slog.Attr {
203
+ // set service.name if the value isn't empty
204
+ if _serviceName != "" {
205
+ result = append (result , slog .String ("service.name" , _serviceName ))
206
+ }
207
+ return result
208
+ }
201
209
210
+ func addTraceContextToLog (span trace.Span , result []slog.Attr ) []slog.Attr {
211
+ result = append (result ,
212
+ slog .String (_traceId , span .SpanContext ().TraceID ().String ()),
213
+ slog .String (_spanId , span .SpanContext ().SpanID ().String ()),
214
+ )
202
215
return result
203
216
}
204
217
218
+ func handleError (span trace.Span , err []error , result []slog.Attr ) []slog.Attr {
219
+ if len (err ) > 0 && err [0 ] != nil {
220
+ span .RecordError (err [0 ])
221
+ span .SetStatus (codes .Error , err [0 ].Error ())
222
+ result = append (result , slog .String ("error" , err [0 ].Error ()))
223
+ }
224
+ return result
225
+ }
226
+
227
+ // New is a function that initializes a new Logger instance,
228
+ // with JSON logging enabled as the logging format.
205
229
func New () * Logger {
206
230
return & Logger {slog .New (slog .NewJSONHandler (os .Stdout , nil ))}
207
231
}
208
232
233
+ // NewWithHandler initializes a new Logger instance with a specified slog.Handler.
234
+ // If no handler is provided (i.e., handler is nil), it calls the New() function
235
+ // to create a Logger with the default setup.
209
236
func NewWithHandler (handler slog.Handler ) * Logger {
237
+ // If handler is nil, create a default Logger with json logging to standard output
210
238
if handler == nil {
211
239
return New ()
212
240
}
241
+
213
242
return & Logger {slog .New (handler )}
214
243
}
215
244
245
+ // WithTracingContext is a method for the Logger struct which takes a context.Context
246
+ // and log parameters, including a span from distributed tracing and (optional) error information.
247
+ // When logging without an error, pass a nil
216
248
func (l Logger ) WithTracingContext (ctx context.Context , level slog.Level , msg string , span trace.Span , err error , attrs ... slog.Attr ) {
217
249
attrs = append (attrs , AddTracingContext (span , err )... )
218
250
l .LogAttrs (ctx , level , msg , attrs ... )
0 commit comments