-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: introduce logger choice via slog #292
Conversation
We have created an issue in Pivotal Tracker to manage this: https://www.pivotaltracker.com/story/show/186934394 The labels on this github issue will be updated when the story is started. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn’t it be better to use an interface that matches the slog/Logger, instead of the concrete type?
Good point. That's a good pattern in general for Go. I think I was influenced by an example (that I can no longer find) that I interpreted as it being "normal" to pass a logger concrete type. But the more I think about it, the more it makes sense to take an interface that's defined in brokerapi. (One of the big issues with I'll have a go at re-working this to make that change. |
There is a snag in using the interface approach. We use the type Logger interface {
With(args ...any) Logger
} Then type Logger interface {
With(args ...any) *slog.Logger
} Is an improvement on a concrete type, but still ties us to the concrete type. But I had been thinking about usages of |
I'm struggeling at the same spot for the last 30 minutes 😂 |
I tried to turn https://github.com/pivotal-cf/brokerapi/blob/main/utils/context.go#L46 around, into a context-aware handler, so that we could have all values within the context. But in the end we would need to set the handler, which is also not going to work without |
In the end people have a dependency to the standard library (anyways) and could provide a custom |
Thanks for trying @elgohr. I also considered not using the |
62d6db8
to
b01172f
Compare
2bd8880
to
5e2146c
Compare
Historically brokerapi has required use of the [`lager`](https://github.com/cloudfoundry/lager) logger. In Go 1.21, structured logging was introduced into the Go standard library via the [`log/slog`](https://pkg.go.dev/log/slog) package, and `slog` [compatability was added](cloudfoundry/lager@4bf4955) to `lager`. `brokerapi` has been modified to require a `slog` logger to be passed rather than a `lager` logger. This allows users a choice of logger. Users who still want to use lager can easily do that using the lager/slog compatability: ```go logger := lager.NewLogger(name) brokerAPI := brokerapi.New(serviceBroker, slog.New(lager.NewHandler(logger)), credentials) ``` And users who want to use `slog` or an `slog`-compatible logger can do that instead. A key advantage is that `lager` is no longer a dependency of this package, which simplifies package management for apps that use brokerapi and other libraries which use `lager`.
Externally we take a *slog.Logger, but internally we wrap this almost everywhere
Previously some of the public API had been altered to take the (internal) Blog logger. This is not correct, so the public API should now consistely take a *slog.Logger, and a Blog logger now implements the slog.Handler interface so can easily be converted to a slog.Logger.
handlers/last_binding_operation.go
Outdated
logger := h.logger.Session(lastBindingOperationLogKey, lager.Data{ | ||
instanceIDLogKey: instanceID, | ||
}, utils.DataForContext(req.Context(), middlewares.CorrelationIDKey, middlewares.RequestIdentityKey)) | ||
logger := h.logger.Session(req.Context(), lastBindingOperationLogKey, blog.InstanceID(instanceID)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this didn't exist before but I think we should log the bindingID here as well since it is the binding last operation
Breaking Changes
This package now accepts a
*slog.Logger
from the Go standard library, rather than a Lager logger. This allows the use of alternative loggers.code.cloudfoundry.org/lager/v3
.New()
,NewWithCustomAuth()
,NewWithOptions()
, and alsoAttachRoutes()
all take a*slog.Logger
apiresponses.FailureResponse
errors with aValidatedStatusCode()
method also take a*slog.Logger
rather than a Lager loggermiddlewares.APIVersionMiddleware
has had theLoggerFactory
field removed, and a new fieldLogger
added with type*slog.Logger
.If you want to continue to use Lager, you can just convert it to a
*slog.Logger
, for which you will need Lager v3.0.3 for example:Reasons
Historically brokerapi has required use of the
lager
logger. In Go 1.21, structured logging was introduced into the Go standard library via thelog/slog
package, andslog
compatability was added tolager
.brokerapi
has been modified to require aslog
logger to be passed rather than alager
logger. This allows users a choice of logger. Users who still want to use lager can easily do that using the lager/slog compatability.And users who want to use
slog
or anslog
-compatible logger can do that instead.A key advantage is that
lager
is no longer a dependency of this package, which simplifies package management for apps that use brokerapi and other libraries which uselager
.Resolves #267