I'm working with the Zap logger library in Go and I'm trying to achieve a custom timestamp generation approach for logging. Specifically, I want to use my own time function instead of relying on the system time.
While I've explored the documentation, I haven't been able to pinpoint the solution for implementing a custom time function effectively.
In essence, I'm aiming for functionality similar to this pseudo-code:
// Sudo code
var tmFn func()time.Time
// more code
logger.SetTimeFunc(tmFn)
I'd appreciate any insights or guidance on how to achieve this customization within the Zap logger library.
You can use
WithClockoption to define your own clock, providing the time function you want. E.g. with a constant clock (always returning a date like2077-01-23T10:15:13Z). This example is based onclock_test.gofromzaplibrary.You can play with that code here: https://go.dev/play/p/8oLQm7mXrlD, and this will print:
From there, you can customize your clock like you need (
Now()andNewTicker()methods) to provide the time you will expect.