rs / zerolog

Zero Allocation JSON Logger
MIT License
10.62k stars 572 forks source link

Double quotes are escaped (while using ConsoleWriter) #504

Open slashtechno opened 2 years ago

slashtechno commented 2 years ago

I'm new to using zerolog. I'm using ConsoleWriter, as it's indented to be viewable to the end-user, while running the program. However, double quotes seem to be escaped.

    logger.Fatal(). // Fatal because this is the last attempt at a ping
        Err(err).
        Str("help", "Privileged pings are disabled. To enable, run \"sudo sysctl -w net.ipv4.ping_group_range=\"0 2147483647\"\" For more information, check https://github.com/prometheus-community/pro-bing#linux").
        Msg("Privileged ping failed")

Outputs the following: 2022-11-05T03:23:29-05:00 FTL Privileged ping failed error="socket: permission denied" help="Privileged pings are disabled. To enable, run \"sudo sysctl -w net.ipv4.ping_group_range=\"0 2147483647\"\" For more information, check https://github.com/prometheus-community/pro-bing#linux"

My logger configuration is logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).Level(zerolog.DebugLevel).With().Timestamp().Logger()

luoming1224 commented 1 year ago

I have a similar problem when using ConsoleWriter. My logger configuration is

        output := zerolog.ConsoleWriter{Out: writer, NoColor: true, TimeFormat: timeFormat}
        output.FormatTimestamp = func(i interface{}) string {
        return fmt.Sprintf("%s", i)
    }
    output.FormatLevel = func(i interface{}) string {
        return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
    }
    output.FormatCaller = func(i interface{}) string {
        return fmt.Sprintf("%s", i)
    }
    output.FormatMessage = func(i interface{}) string {
        if i == nil {
            return ""
        }
        return fmt.Sprintf("msg:%s,", i)
    }
    output.FormatFieldName = func(i interface{}) string {
        return fmt.Sprintf("%s:", i)
    }
    output.FormatFieldValue = func(i interface{}) string {
        return fmt.Sprintf("%s,", i)
    }
    output.FormatErrFieldName = func(i interface{}) string {
        return fmt.Sprintf("%s:", i)
    }
    output.FormatErrFieldValue = func(i interface{}) string {
        return fmt.Sprintf("%s,", i)
    }

Configure logger in gin, the outputs following:

2023-03-01 11:32:26 | INFO  | logger.go:62 body_size:43, client_ip:1x.xx.xx.x8, latency:"573.283µs", method:GET, path:/ping, status_code:200,
2023-03-01 11:32:26 | INFO  | logger.go:62 body_size:43, client_ip:1x.xx.xx.x8, latency:1.573308ms, method:GET, path:/ping, status_code:200,
2023-03-01 11:32:26 | INFO  | logger.go:62 body_size:43, client_ip:1x.xx.xx.x8, latency:"132.877µs", method:GET, path:/ping, status_code:200,

in latency,There are double quotes when the time is microseconds, and no double quotes when the time is milliseconds;Others, such as method/path/client_ ip is not enclosed with double quotation marks.

mitar commented 1 year ago

Those are two separate but similar issues. µ is unicode so you get quotation marks. And escaping quotes inside strings is useful so that you know that they are part of the string and not the next field.

I think both of those cases are "good enough".