-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: custom slog handlers for modules (log contextual data) #7346
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
Conversation
6c5c95c to
9ce4728
Compare
9ce4728 to
a62d829
Compare
mholt
left a comment
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.
Thanks Kevin!
Just so I understand, this doesn't affect MOST of Caddy's logs, i.e. won't noticeably impact their performance or change them -- just the logs that are emitted without zap, right?
| type slogHandlerFactory func(handler slog.Handler, core zapcore.Core, moduleID string) slog.Handler | ||
|
|
||
| var ( | ||
| slogHandlerFactories []slogHandlerFactory | ||
| slogHandlerFactoriesMu sync.RWMutex | ||
| ) |
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 is an annoying request, but how would you feel if we drop the "factory" thing? 😆 I just... it feels like Java.
What about like slogHandlerWrapper instead? Or, maybe even just slogHandler? I am not 100% sure since I haven't dived into slog too much yet to be that familiar with it.
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.
Actually, this is a list of factory to create log/slog.Handler instances when needed (these are "wrapping" the main instance by implementing the decorator pattern). Removing the factory suffix will be confusing IMHO.
|
Right. |
mholt
left a comment
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.
Alrighty -- sounds good then. Thank you!
With this patch, contextual data can be added to all logs generated by a logger retrieved using
context.Slogger().For instance, this allows FrankenPHP and Mercure to include OpenTelemetry's traceID and spanID to all logs they generate using the logger created by Caddy.
Modules can also register handlers to add contextual data of their own (like the Mercure update ID, for instance).
Assistance Disclosure
No AI was used.