

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Logging in Go
incomplete
2: Use the Logger
incomplete
3: Logging Requests
incomplete
4: Global Logger vs. Dependency Injection
incomplete
5: Logger Configuration
incomplete
6: Logger Failure
incomplete
7: Buffered Logging
incomplete
8: Logger Cleanup
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Our current logger, especially when writing to a file, is relatively slow. Every time we log a message, it writes to disk, no matter how large or small the message is. That's potentially a lot of disk I/O, and it can really slow down our entire application because many small writes are much slower than a few large writes.
Observability being the reason our app is slow is, frankly, embarrassing.
One solution is to use a buffered writer like bufio.Writer around the file. This allows us to write log messages to an in-memory buffer, and then that buffer is only written to disk when it's full.
bufferedFile := bufio.NewWriterSize(file, 1024)
Buffer file logging writes.
LINKO_LOG_FILE=linko.access.log go run .
Run and submit the CLI tests from the root of the Linko repo.
If you create the buffered logger the way I did, it will introduce a subtle bug... but don't worry we'll fix it later!