Writing an efficient
Logger in Go
Who am I?
Damiano Petrungaro
Damiano Petrungaro
Italy
source: www.vidiani.com
source: www.greenparkmadama.it
source: www.milanotoday.it
Me everyday:
Learning
- Learn to define idiomatic Go APIs
- Learn to implement a safe and fast logger
- Learn to benchmark and profile your code
- Learn to write a faster JSON encoder
How to
- Write a basic logger
- Add benchmarks for the logger
- Add APIs to add fields to logs
- Write logs as JSON
-
Address JSON unmarshal failure
- Add APIs to add trace IDs to logs
Write a basic logger
- Add logger interface
- Add tests
- Add logger implementation
Add benchmarks to the logger
- Add benchmarks for the logger
- Run and analyze memory profile
- Run and analyze CPU profile
Add APIs to add fields to log
- Add `With` method to interface
- Add tests
- Add benchmark: map vs slice
- Add slice implementation
- Add `With` logger benchmarks
- Check benchmark and profile delta
Write logs in JSON
- Change tests
- Change logger implementation
- Add other libraries to the benchmark
- Check benchmarks and profile data
- Break the API adding an invalid `any`
Address JSON unmarshal failure
- Add bug as test case
- Add APIs for a safer logger
- Check benchmarks and profile data
- Improve implementation if needed
Add APIs to add trace IDs to logs
- Add implementation as a decorator
- Change tests
- Change logger implementation
Recap
- We wrote an idiomatic logger
- We based our decisions on data
- We can add more decorators
- We could try another field implementation
- We should pass a real-life writer (os.Stdout)
- Visit the library damianopetrungaro/golog
Next steps
Useful links
Twitter: @damiano_dev
LinkedIn: /in/damianpetrungaro
Email: damianopetrungaro@gmail.com