One last note in the performance section…
How does logging work in a concurrent system?
Performance considerations
…
The built-in handlers acquire a lock before calling io.Writer.Write to ensure that exactly one Record is written at a time in its entirety. Although each log record has a timestamp, the built-in handlers do not use that time to sort the written records. User-defined handlers are responsible for their own locking and sorting.
So three points fall out from this:
- The built-in logging handlers ensure it’s safe to log from multiple goroutines concurrently. You’ll never get a partial log from one interwoven with a log from another. This won’t happen:
2022/11/08 15:28:26 INFO hello 2022/11/08 15:28:27 INFO hello count=3count=3
- Timestamps are not used for sorting. So in theory, this could happen:
2022/11/08 15:28:28 INFO hello count=3
2022/11/08 15:28:27 INFO hello count=3
- If you’re implementing your own logger, these guarantees don’t apply, unless you implement them yourself.