Why is log.With(fields) slower than log.Info(msg, fields) #1112
-
consoleEncoder uses jsonEncoder. jsonEncoder writes fields immediately in the buffer. Then it copies the buffer when log.With is called. This behaviour is very unfortunate if logger is never called. And that is quite possible if logging is used mostly for errors. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello! Yes, indeed log.With is eager at serializing because it's meant for loggers that are meant to be
logger.With shares the serialized form of those fields between all uses of the logger. For fields that are not meant to be re-used across loggers, we recommend this pattern:
This will serialize those fields only if that log message is actually posted. We'll try to add something about this to the documentation, but to summarize:
We're actually trying to build a small linter to catch these cases; we're hoping to open source it once it's ready. Hope this helps! Closing because answered. Feel free to re-open or create a new ticket. |
Beta Was this translation helpful? Give feedback.
Hello! Yes, indeed log.With is eager at serializing because it's meant for loggers that are meant to be
logger.With shares the serialized form of those fields between all uses of the logger.
For fields that are not meant to be re-used across loggers, we recommend this pattern:
This will serialize those fields only if that log message is actually posted.
We'll try …