newrelic / go-agent

New Relic Go Agent
Apache License 2.0
763 stars 297 forks source link

UNKNOWN log levels when using the native log package #842

Closed hammady closed 5 months ago

hammady commented 7 months ago

Summary

Go native log package does not support log levels. However, starting from go 1.21, a new standard package was introduced: log/slog which supports structured logging as well as log levels. I was able to integrate the go agent with slog to report logs in context. However, because log levels happen outside the Newrelic logWriter, records are all reported without the Severity attribute and hence show as UNKNOWN in the APM UI.

image

Desired Behaviour

I would like the go agent to be able to be integrated deeper with slog so that the correct severity (level) is reported with each record.

Example from the ruby agent:

image

Additional context

Showing log severity within APM UI is useful in observability like all other agents, nothing specific here. It is only missing because it was not supported in the standard log package before, but now it is supported through log/slog

Note: Here is how I did the integration with slog:

writer := logWriter.New(os.Stdout, app)
options := slog.HandlerOptions{
    Level: utils.GetLogLevel(),
}
logger := slog.New(slog.NewTextHandler(writer, &options))
// set default logger for top-level functions like log.Println(), log.Printf(), etc.
slog.SetDefault(logger)
iamemilio commented 5 months ago

Please take a look at the new nrslog package!

hammady commented 5 months ago

@iamemilio thanks for the follow-up. I can see nrslog has been added just 3 weeks ago, great job! I had to switch to Logrus to get the complete functionality of structured logging + NR context.