rs / zerolog

Zero Allocation JSON Logger
MIT License
10.53k stars 571 forks source link

ConsoleWriter TimeFormat #213

Open skaldesh opened 4 years ago

skaldesh commented 4 years ago

I created a new logger like this:

l := zerolog.New(zerolog.ConsoleWriter{
    Out:        os.Stderr,
    TimeFormat: "2006-01-02T15:04:05.999Z07:00",
}).With().Timestamp().Logger()

However, the time logged by it looked like this: 2020-02-05T10:27:38+01:00 ERR server handle new connection

Looking at the code and how the console writer formats the time, it turned out I had to globally set the time field format to something that is formatting AT LEAST as precise, as the format on the ConsoleWriter that I want. This means setting: zerolog.TimeFieldFormat = time.RFC3339Nano yielded the correct time output: 2020-02-05T10:27:38.921+01:00 ERR server handle new connection:

Is this how it is intended? Setting the global time field of course affects all my loggers...

rs commented 3 years ago

I'm not sure to understand the issue. You want to log without nano precision but output to the console with nano precision?

skaldesh commented 3 years ago

Puh, its some time ago I needed this. I think what surprised me back then was that you had to set the time format twice...
So I expected that

l := zerolog.New(zerolog.ConsoleWriter{
    Out:        os.Stderr,
    TimeFormat: "2006-01-02T15:04:05.999Z07:00",
}).With().Timestamp().Logger()

is enough, but as pointed out, it does not work on its own.

EDIT:

Setting the global time field of course affects all my loggers...

This is also a problem, isn't it?

mitar commented 1 year ago

I have seen multiple issues now with people being confused how console writer timestamps work. Probably README should contain some entry about the fact that console writer can format timestamps only with information available from zerolog.TimeFieldFormat.

Setting the global time field of course affects all my loggers... This is also a problem, isn't it?

Is it? What is the real use case where you would have in one program timestamps in different formats? For some things global settings are reasonable.