-
Notifications
You must be signed in to change notification settings - Fork 42
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
Confusing to use in an application already using log/slog
#28
Comments
Aside from manually creating the It also seems to use |
It shouldn’t touch global slog, and you can pass options.Writer. it certainly could be better and more carefully designed, but I actually don’t have time and it works for my purposes. My suggestion is to submit a PR, or write your own package @diamondburned |
Should a PR make the v3 breaking changes described in this issue, or should it make the changes without being breaking? Part of the current issue is the fact that the README documents |
FWIW, I had configured log level to DEBUG in my program, then switched to the new httplog, and all DEBUG messages were gone until I explicitly re-defined the log level to DEBUG in the call of NewLogger. |
An additional feature request here: I'd like to get the |
may i suggest to just leave NewLogger as is but remove it from the docs and create a new function? |
I ran across this as well, and managed to work around it by using this as my middleware:
Definitely a work-around, but it seems to be doing the job for now. |
hey guys, @stephenafamo @diamondburned @dropwhile @bronger https://github.com/go-chi/httplog/pull/43/files this fixes the default slogger being modified, the json options etc work... normally you would enable json logging in prod since its structured logging which is ingested by cloud providers... |
@diamondburned it logs on os.StdOut by default on the default pretty logger which is meant for dev purposes... and well for prod you should make it os.StdErr imo... which is why the option exists :) |
@stephenafamo i agree, ill work on including an optional slogger in the create httplog function, so that you can inject your own slogger, other than that what else do you feel is wrong with the structure? |
Thanks for taking a look at this @Shubhaankar-Sharma I have nothing else to add to what was in my original comment.
|
Hi everyone, I've created a prototype for Here's the repo you can play around with: It’s a brand new implementation that has Please take a look at it, run the I'll create a PR against this repo sometime next week. But since it's a complete rewrite, it will likely be difficult to review the changes. cc @stephenafamo @Shubhaankar-Sharma @diamondburned @dropwhile @aep @judofyr @bronger Best, |
Works fine on my site, thank you! |
@VojtechVitek: Very nice work, but this doesn't quite work for mine use case. In this case I already have an func (l *Logger) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
+++ logger := l.opts.GetLoggerFromContext(ctx)
log := &Log{
Level: l.opts.Level,
Req: r,
LogRequestBody: l.opts.LogRequestBody,
LogResponseBody: l.opts.LogResponseBody,
} I'm not quite sure what's the best design for this would be. One potential variant could be to do something like: func Middleware(getLogger func(ctx context.Context) *slog.Logger) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := getLogger(ctx)
// Same code as before
}
}
} |
Hi @judofyr, see this message in the official
The proposed https://github.com/golang-cz/httplog repo follows the same decision. It doesn't pass logger via context but it provides a custom (extendable) handler instead, where you can print context values as additional log attributes. In case of HTTP middleware, this is even more complicated, since the top-most middleware (where we print the request log) doesn't have access to the context values created downstream, as the context is unwrapped on the way back up from the final HTTP handler. That is why we introduced r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Set additional attribute(s) on the request log:
httplog.SetAttrs(ctx, slog.String("userId", "id"))
w.Write([]byte("."))
}) I'm not 100% sure what your use-case is, but I think you might be after this: r.Get("/do-work", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Create a custom logger including request attributes from httplog:
customLogger := httplog.WithAttrs(ctx, slog.String("op", "do-work"))
// Pass it down to your implementation:
worker := worker{logger: customLogger}
worker.DoWork(ctx)
// Or if you want to continue passing loggers via context yourself:
ctx = withLogger(ctx, customLogger)
doWork(ctx)
// ...
}) Does this help? |
It is likely that an application using
slog
will want to use its own pre-configured*slog.Logger
or provide aslog.Handler
that a logger should be created from.The design of the package makes it confusing to do this.
Sure, I can manually create a
httplog.Logger
, but the options mix what is required for "configuring the logger" and what is used in logging.I would suggest that the package asks only for a
slog.Handler
and only uses options to configure what to log:I understand that my changes would be breaking (so that means a
v3
so soon after av2
), but I think it becomes easier to use with other slog compatible packages.The text was updated successfully, but these errors were encountered: