Custom timestamp field #1129
-
I'm trying to customise the time stamp field as mentioned in the last comment of this thread.
But it seems the content is outdated already. I couldn't locate |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 2 replies
-
Those APIs are all extremely outdated - they predate zap's 1.0 release. Try starting with the current GoDoc instead. |
Beta Was this translation helpful? Give feedback.
-
Not enough documentation on timestamp usage. I've moved to a different logging library. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Cool - best of luck! |
Beta Was this translation helpful? Give feedback.
-
For the sake of those who come to this issue trying to configure a custom timestamp, here is an example of how to do it with current API:
|
Beta Was this translation helpful? Give feedback.
-
FYI smaller way of doing a custom config: loggerConfig := zap.NewProductionConfig()
loggerConfig.EncoderConfig.TimeKey = "timestamp"
logger, err := loggerConfig.Build() |
Beta Was this translation helpful? Give feedback.
-
https://blog.sandipb.net/2018/05/03/using-zap-creating-custom-encoders/ |
Beta Was this translation helpful? Give feedback.
-
use a human-readable time format: var cfg zap.Config = zap.NewProductionConfig()
// cfg.OutputPaths = []string{"judger.log"}
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
logger, err := cfg.Build() |
Beta Was this translation helpful? Give feedback.
-
I think this is the best way, as you can use any Go time layout string you want: config := zap.NewProductionConfig() // or zap.NewDevelopmentConfig() or any other zap.Config
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339) // or time.RubyDate or "2006-01-02 15:04:05" or even freaking time.Kitchen
logger, err := config.Build() For more information on how to format the layout string, check https://pkg.go.dev/time#pkg-constants |
Beta Was this translation helpful? Give feedback.
-
I appreciate you all following up on this thread. TBH I almost left zap as well because I thought this should be simple and virtually every top google link was not helpful. I would assume that everyone who looks to use your framework wants to do this since the default is not that helpful. I would recommend maybe putting this simple example in your README. I've used several framework across several different languages and pretty new to Go but it was painful to finally find this post with the answer i needed |
Beta Was this translation helpful? Give feedback.
-
@rking-bennie thank you for the feedback. Appreciate the patience and apologies for the difficulties you ran into. Couple of things:
Thanks. |
Beta Was this translation helpful? Give feedback.
-
Please can we have this properly documented. How on earth did such a good library as this, release it with poor representation of timestamp? I am new to zap and the first thing that horified me was the timestamp fiasco and to make things worse, its not clearly documented how to fix it. Good job I stumbled upon this discussion. |
Beta Was this translation helpful? Give feedback.
I think this is the best way, as you can use any Go time layout string you want:
For more information on how to format the layout string, check https://pkg.go.dev/time#pkg-constants