-
Notifications
You must be signed in to change notification settings - Fork 1.4k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom timestamp field #485
Comments
Those APIs are all extremely outdated - they predate zap's 1.0 release. Try starting with the current GoDoc instead. |
Not enough documentation on timestamp usage. I've moved to a different logging library. Thanks. |
Cool - best of luck! |
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:
|
FYI smaller way of doing a custom config: loggerConfig := zap.NewProductionConfig()
loggerConfig.EncoderConfig.TimeKey = "timestamp"
logger, err := loggerConfig.Build() |
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() |
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 |
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 |
@rking-bennie thank you for the feedback. Appreciate the patience and apologies for the difficulties you ran into. Couple of things:
Thanks. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I'm trying to customise the time stamp field as mentioned in the last comment of this thread.
#33
But it seems the content is outdated already. I couldn't locate
zap.RFC3339Formatter
andzap.NewJSONEncoder
is moved tozapcore.NewJSONEncoder
. An example usage with the new API changes would be a great thing to have.The text was updated successfully, but these errors were encountered: