apple/swift-log

StreamLogHandler is too quick to prettify metadata

PeterAdams-A opened this issue · 5 comments

I've noticed that StreamLogHandler prettifys meta data as soon as it is set.

This seems like it is probably wasteful.

  1. If you set multiple metadata keys (through subscript) it will re-prettify after each key.
  2. If you log with extra metadata the pre-prettified data can't be used.
  3. If you never log [at a level above the logLevel] the prettified data will never be used.

I propose that it would be better to prettify on first usage rather than when metadata is set.

The only downside I can think of is that this possibly shifts the prettification to a more critical moment in execution.

The sad complexity I'd not spotted is that StreamLogHandler would of course have to become mutable when logging to achieve this which doesn't seem ideal.

ktoso commented

Yeah it won't be just a trivial change I think -- do we have benchmarks that prove this will improve things in workloads you are encountering?

I think absolutely nobody should use StreamLogHandler, apart from basic testing of their stuff before they settled on a real log handler.

For performance tests people should use NoOpLogHandler, for real world stuff they should use a real log handler.

ktoso commented

Yeah, that's a true statement as well. Sounds like we should close as won't fix which I'm okey with.

To be honest the biggest "issue" is that we even ship this logger... every single time it comes to bite us eh... :-\

I'll close as I've come to the realisation that this is both the wrong place to fix this and hard.

I think the mistake was making the provided implementation anything other than the most naive possible example.