Skip to content

Commit c8c2cb7

Browse files
authored
Merge pull request #48 from vincentfree/add-convert-func
Refactor slog.go for better code readability
2 parents 02752d7 + c9d57c2 commit c8c2cb7

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

otelslog/slog.go

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,25 @@ func AddTracingContext(span trace.Span, err ...error) []slog.Attr {
132132
// AddTracingContextWithAttributes lets you add the trace context to a structured log, including attribute.KeyValue's to extend the log
133133
func AddTracingContextWithAttributes(span trace.Span, attributes []attribute.KeyValue, err ...error) []slog.Attr {
134134
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)
140138

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...)
149141

150-
attrs := attributes
151-
attrs = append(attrs, _attributes...)
142+
// add attributes when global or passed in attributes are > 0
143+
result = ConvertToSlogFormat(attributes, result)
152144

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 {
156154
switch attr.Value.Type() {
157155
case attribute.STRING:
158156
result = append(result, slog.String(_attrPrefix+"."+string(attr.Key), attr.Value.AsString()))
@@ -198,21 +196,55 @@ func AddTracingContextWithAttributes(span trace.Span, attributes []attribute.Key
198196
}
199197
}
200198
}
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+
}
201209

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+
)
202215
return result
203216
}
204217

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.
205229
func New() *Logger {
206230
return &Logger{slog.New(slog.NewJSONHandler(os.Stdout, nil))}
207231
}
208232

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.
209236
func NewWithHandler(handler slog.Handler) *Logger {
237+
// If handler is nil, create a default Logger with json logging to standard output
210238
if handler == nil {
211239
return New()
212240
}
241+
213242
return &Logger{slog.New(handler)}
214243
}
215244

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
216248
func (l Logger) WithTracingContext(ctx context.Context, level slog.Level, msg string, span trace.Span, err error, attrs ...slog.Attr) {
217249
attrs = append(attrs, AddTracingContext(span, err)...)
218250
l.LogAttrs(ctx, level, msg, attrs...)

0 commit comments

Comments
 (0)