rs / zerolog

Zero Allocation JSON Logger
MIT License
10.39k stars 565 forks source link

The timestamp color (colorDarkGray) is not readable on some terminals #246

Open bojanz opened 4 years ago

bojanz commented 4 years ago

I use a non-customized zsh + Pure terminal. Pure has a dark background color, making the ConsoleWriter's timestamp color hard to read: https://www.dropbox.com/s/qdj9t1plv977s4g/zerolog%20terminal.png?dl=0

This was introduced in #131.

Could I suggest not coloring the timestamp at all, like Zap does? After all, the primary goal for the coloring logic is to differentiate the levels.

Hendrik-H commented 4 years ago

I'm using a terminal with a "Solarized" color scheme and the timestamp seems to be exactly in the background color and thus not visible at all.

ningzio commented 3 years ago

could we disable color on timestamp field? It's really hard to read on dark background terminal

aep commented 2 years ago

confusing that this is still open. is it a WONTFIX?

rs commented 2 years ago

It’s a won’t fix by itself :)

bojanz commented 2 years ago

@rs Happy to send in a PR if you agree with the proposed fix (stop coloring the timestamp).

rs commented 2 years ago

Go ahead

ningzio commented 2 years ago

the default timestamp Formatter implementation is written in here

we can write our own Formatter to disable color

func main() {
    consoloLog := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
        w.FormatTimestamp = consoleFormatTimestamp(w.TimeFormat)
    })
}

func consoleFormatTimestamp(timeFormat string) zerolog.Formatter {
    return func(i interface{}) string {
        t := "<nil>"
        switch tt := i.(type) {
        case string:
            ts, err := time.Parse(zerolog.TimeFieldFormat, tt)
            if err != nil {
                t = tt
            } else {
                t = ts.Format(timeFormat)
            }
        case json.Number:
            i, err := tt.Int64()
            if err != nil {
                t = tt.String()
            } else {
                var sec, nsec int64 = i, 0
                switch zerolog.TimeFieldFormat {
                case zerolog.TimeFormatUnixMs:
                    nsec = int64(time.Duration(i) * time.Millisecond)
                    sec = 0
                case zerolog.TimeFormatUnixMicro:
                    nsec = int64(time.Duration(i) * time.Microsecond)
                    sec = 0
                }
                ts := time.Unix(sec, nsec).UTC()
                t = ts.Format(timeFormat)
            }
        }
        return t
    }
}