zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

ConsoleWriter writes %!S from log.Logger

Open jboelter opened this issue 5 years ago • 5 comments

The console log writer doesn't handle input from the default log (or any Write) when redirected into zerolog and writes %!S as the log level.

Example

package main

import (
        "log"
        "os"

        "github.com/rs/zerolog"
)

func main() {

        output := zerolog.ConsoleWriter{Out: os.Stderr}
        logger := zerolog.New(output).With().Timestamp().Logger()

        l := log.New(logger, "", 0)
        l.Println("Hello World")
}

Actual output

>go run main.go
3:40PM %!S Hello World

Need to add a nil check in consoleDefaultFormatLevel -- not sure if there are other codepaths that should be addressed.

		if i == nil {
			l = "LOG"
		}

*console.go consoleDefaultFormatLevel

func consoleDefaultFormatLevel(noColor bool) Formatter {
	return func(i interface{}) string {
		var l string
		if i == nil {
			l = "LOG"
		} else if ll, ok := i.(string); ok {
			switch ll {
			case "debug":
				l = colorize("DBG", colorYellow, noColor)
			case "info":
				l = colorize("INF", colorGreen, noColor)
			case "warn":
				l = colorize("WRN", colorRed, noColor)
			case "error":
				l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
			case "fatal":
				l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
			case "panic":
				l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
			default:
				l = colorize("???", colorBold, noColor)
			}
		} else {
			l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
		}
		return l
	}
}

jboelter avatar Apr 22 '19 22:04 jboelter

I opened #147 as a solution to this problem.

dcarbone avatar May 23 '19 18:05 dcarbone

I believe it has been fixed by recent version. Can you please confirm?

rs avatar May 23 '19 18:05 rs

I can confirm 1.14 prints ??? rather than %S, which I can see the benefits of over my pr.

dcarbone avatar May 23 '19 18:05 dcarbone

Are you ok to close this ticket then?

rs avatar May 23 '19 20:05 rs

@jboelter was the issuer of this ticket. i have closed my PR.

dcarbone avatar May 23 '19 21:05 dcarbone