zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

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

Open bojanz opened this issue 4 years ago • 7 comments

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.

bojanz avatar Jun 25 '20 17:06 bojanz

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.

Hendrik-H avatar Aug 24 '20 16:08 Hendrik-H

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

ningzio avatar May 26 '21 06:05 ningzio

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

aep avatar Oct 30 '21 19:10 aep

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

rs avatar Oct 31 '21 01:10 rs

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

bojanz avatar Oct 31 '21 09:10 bojanz

Go ahead

rs avatar Oct 31 '21 11:10 rs

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
	}
}

ningzio avatar Nov 01 '21 03:11 ningzio