log/slog: logger discards errors from handler #66579
Labels
Documentation
Issues describing a change to documentation.
NeedsFix
The path to resolution is known, but the work has not been done.
Go version
go version go1.21.0 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
What did you see happen?
The logger disregards errors from the handler, and there is a method to explicitly set an error handling mechanism.
See https://github.com/golang/go/blob/master/src/log/slog/logger.go#L257
What did you expect to see?
Slog has introduced a philosophy by adopting a frontend and backend design, where the logger serves as the frontend and the handler as the backend. However, the backend/handler may need to write logs to a remote logger, so handling those errors is essential for users to understand why some logs are not being sent or logged, regardless of what the handler is doing. While it's acceptable for the frontend/logger to ignore these errors by default, users should at least be allowed to set their error handler.
When using the stdlib log, there is no way to set an error handler, but that is acceptable as that log package only adopted a different design. If a user intended to handle errors, they could wrap the io writer with a custom error handler. However, wrapping the slog handler and passing it to slog is considered overkill due to the interface and conflicts with the slog design and philosophy.
Looking at other packages like flag, which offers error handling options like continue or panic, and httputil, where the proxy allows handling errors by setting a function explicitly, it would be beneficial to update the docs to clarify that the slog logger ignores errors from the handler (if this information is not already present). Additionally, exposing an error handling function to allow users to set it explicitly would be beneficial.
This may need to be converted from a bug report to a proposal, but starting the discussion here before moving it to the proposal boards could be helpful.
The text was updated successfully, but these errors were encountered: