Skip to content

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

Closed
apremalal opened this issue Aug 1, 2017 · 10 comments
Closed

Custom timestamp field #485

apremalal opened this issue Aug 1, 2017 · 10 comments

Comments

@apremalal
Copy link

I'm trying to customise the time stamp field as mentioned in the last comment of this thread.

#33

logger := zap.New(
  zap.NewJSONEncoder(zap.RFC3339Formatter("@timestamp")),
  zap.ErrorLevel,
  zap.Output(someFile),
)

But it seems the content is outdated already. I couldn't locate zap.RFC3339Formatter and zap.NewJSONEncoder is moved to zapcore.NewJSONEncoder. An example usage with the new API changes would be a great thing to have.

@akshayjshah
Copy link
Contributor

Those APIs are all extremely outdated - they predate zap's 1.0 release.

Try starting with the current GoDoc instead.

@apremalal
Copy link
Author

Not enough documentation on timestamp usage. I've moved to a different logging library. Thanks.

@akshayjshah
Copy link
Contributor

Cool - best of luck!

@dmuntean
Copy link

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:

atom := zap.NewAtomicLevel()

encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = "timestamp"
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder

logger := zap.New(zapcore.NewCore(
	zapcore.NewJSONEncoder(encoderCfg),
	zapcore.Lock(os.Stdout),
	atom,
))

defer logger.Sync()

atom.SetLevel(zap.DebugLevel)

@grosser
Copy link

grosser commented Sep 10, 2019

FYI smaller way of doing a custom config:

loggerConfig := zap.NewProductionConfig()
loggerConfig.EncoderConfig.TimeKey = "timestamp"
logger, err := loggerConfig.Build()

@gopaltirupur
Copy link

@bytemain
Copy link

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()

@happybydefault
Copy link

happybydefault commented May 7, 2021

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

@rking-bennie
Copy link

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

@sywhang
Copy link
Contributor

sywhang commented Jul 12, 2022

@rking-bennie thank you for the feedback. Appreciate the patience and apologies for the difficulties you ran into.

Couple of things:

  1. I'll turn this thread into a discussion so that it can be more easily discovered.
  2. We are discussing internally to gather effort for creating a documentation site that goes over core concepts and HOWTOs for some of the core open sourced frameworks and libraries, and Zap is on top of the mind for us. We will keep you updated via discussions announcement when that effort gets consolidated.

Thanks.

@uber-go uber-go locked and limited conversation to collaborators Jul 12, 2022
@sywhang sywhang converted this issue into discussion #1129 Jul 12, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

9 participants