log: add option to place prefix text immediately before log text #32062
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
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
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:
Add a new log flag:
@@ -40,6 +40,7 @@ const ( Llongfile // full file name and line number: /a/b/c/d.go:23 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone + LmsgPrefix // place prefix text immediately before the message instead of the beginning of the line LstdFlags = Ldate | Ltime // initial values for the standard logger )
(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
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.
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?