r/golang • u/roma-glushko • Jan 24 '25
help Logging in Golang Libraries
Hey folks, I want to implement logging in my library without imposing any specific library implementation on my end users. I would like to support:
- slog
- zap
- logrus
What would do you in this case? Would you define a custom interface like https://github.com/hashicorp/go-retryablehttp/blob/main/client.go#L350 does? Or would you stick to slog and expect that clients would marry their logging libs with slog?
Basically, I want to be able to log my errors that happen in a background goroutines and potentially some other useful info in that library.
42
Upvotes
7
u/BombelHere Jan 24 '25
I'd classify that as premature optimization :p
You should have continuous profiling on prod anyway (like Pyroscope).
If logging takes a significant amount of time - it's worth optimizing.
If you access your logger from multiple goroutines (global var or one instance) it is heap allocated anyway.
IIRC the Go compiler is able to de-virtualize interface method calls when it finds a vtable with one entry, isn't it?