containers / image

Work with containers' images
Apache License 2.0
866 stars 377 forks source link

Support for structured logging (using `log/slog`) #2217

Open cfiderer opened 10 months ago

cfiderer commented 10 months ago

As of now, the containers/image library uses the sirupsen/logrus package for logging. This has a major drawback: when performing image activities for several images in different goroutines in parallel, it is not possible to mark or tag the log messages such that they an be attributed to the right activity.

Go 1.21 introduced structured logging (see Structured Logging with slog), allowing logger instances to be used as arguments and attributed to specific activities.

Please consider adding structured loggers to the API calls in the containers/image library.

mtrmac commented 10 months ago

Thanks for reaching out.

At this point, c/image only requires Go 1.19, so this is not yet possible (a migration to Go 1.20 could happen pretty soon, but we are probably going to be stuck on 1.20 support for the supported lifetime of Fedora 38).

Longer-term, yes, logrus is in maintenance mode, and migrating to the standard-library API is the most obvious replacement choice.

cfiderer commented 10 months ago

About the support for older Go versions: the github.com/sagikazarmark/slog-shim package provides a means to migrate to slog before adopting Go 1.21.

cfiderer commented 6 months ago

Looks as if you are at Go 1.21 now... with #2377

mtrmac commented 5 months ago

First the callers need to be able to handle a library that uses both logging frameworks, something vaguely like https://github.com/mtrmac/skopeo/tree/logrus-deprecate .

It’s… tedious work and for interactive use, the output is not particularly pleasant. For non-debug output progress output, manually using fmt.Sprintf() instead of the structured fields is fairly attractive in some cases.

cfiderer commented 5 months ago

For Skopeo (and other interactive programs), using fmt.Sprintf() is surely attractive and a lightweight solution.

We use Logrus hooks to transport logging messages into the log/slog environment, so we are (at least) able to collect all log data.

But we use the c/image library in a Kubernetes operator, and we perform several (up to eight) image operations im parallel. In that environment, it is highly desirable to use context-aware logging to separate the sequential activities for each image operation. Furthermore, logging frameworks (like Fluentd) are already collecting structured logs and make them available for queries.