-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
log: add option to place prefix text immediately before log text #32062
Comments
This bites me every time I've tried to use the log prefix. Then I re-remember where it inserts it and I stop using the log prefix and write my own log wrapper func instead. |
This hits me each time I try to use this too. I always expect the date to be before the prefix, not after. A bit to say "put the date and file (if there) before the prefix" makes sense to me, but I am not sure about the name. LmsgPrefix makes it sound like you just want to show the prefix, and the prefix is always shown. /cc @robpike |
@robpike, thoughts about what the API change should be? |
@rsc Does it need API? You have SetFlags. Add a flag with boolean effect like LdateFirst. Haven't thought about it much though. |
Yeah, LmsgPrefix isn't great. I was going to say that the problem with LdateFirst is that it's not just that the date should be first: the file:line (if present) should also precede the prefix text.
But now that I've written it out, I'm not sure. Either way kind of seems fine to me: the important part is that the date is first. So I'd be okay with LdateFirst and having it output
|
I think the point of moving things was to attach the prefix more clearly to the message text being printed. That would mean putting it after the file:line too. That would align nicely with the original request for Lmsgprefix. (Not LmsgPrefix; see Lshortfile, Llongfile etc. LstdFlags is the odd man out.) Any objections to Lmsgprefix? /cc @robpike |
Fine with me. |
Marking accepted based on discussion. |
Change https://golang.org/cl/186182 mentions this issue: |
Does LprefixAfterHeaders sound like a better name than LmsgPrefix? |
Change https://golang.org/cl/217126 mentions this issue: |
Updates #32062 Updates #36878 Change-Id: I06c7c9a9d253177155a6d46d58231ce26e659757 Reviewed-on: https://go-review.googlesource.com/c/go/+/217126 Reviewed-by: Brad Fitzpatrick <[email protected]>
At my company we use the log package extensively. A shortcoming we often encounter is that we want to include a text prefix on every log line before the logged text but after the automatic prefix. That is, if you have
You get
And we often want
One place this comes up is in scenarios where we are processing different items and we want to include context about them in the log messages:
(This is tangentially related to creating a "child" logger, which is possible as of Go 1.12.)
Without any way to put the prefix where we want, we end up needing some workaround such as passing an additional
logPrefix string
around and remembering to include it in every log message, or else using custom logging code.I think that if the standard logger became an interface then our code would mostly pass around that interface and it would be easy to implement this cleanly ourselves. But that has proved to be a thorny issue and little progress has been made in the past few years. In the meantime, here are two alternatives for addressing this issue today:
Idea 1
Add a new log flag:
Idea 2
(It would need better documentation.) There need not be top-level MessagePrefix/SetMessagePrefix functions because this type of prefix doesn't make much sense globally. Then we would do
The text was updated successfully, but these errors were encountered: