zerolog
zerolog copied to clipboard
ConsoleWriter writes %!S from log.Logger
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
}
}
I opened #147 as a solution to this problem.
I believe it has been fixed by recent version. Can you please confirm?
I can confirm 1.14 prints ???
rather than %S
, which I can see the benefits of over my pr.
Are you ok to close this ticket then?
@jboelter was the issuer of this ticket. i have closed my PR.