Change location level and timestamp Zerolog golang

王林
Release: 2024-02-12 18:09:05
forward
1255 people have browsed it

更改位置级别和时间戳 Zerolog golang

Question content

I'm using Zerolog for logging but I'm getting complaints because the log format is different from before and I'm trying to refactor from another language to golang. Is it possible to change the location level and timestamp?

This is my code: `

consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: 
true, TimeFormat: time.RFC3339}

consoleWriter.FormatLevel = func(i interface{}) string {
    return strings.ToUpper(fmt.Sprintf("[ %-6s] -", i))
}
consoleWriter.FormatTimestamp = func(i interface{}) string {
    return strings.ToUpper(fmt.Sprintf("[%s]", i))
}

if cfg.Logger.WriteLogger {
    multi = zerolog.MultiLevelWriter(consoleWriter, file)
} else {
    defer file.Close()
    multi = zerolog.MultiLevelWriter(consoleWriter)
}

logger := zerolog.New(multi).Level(zerolog.TraceLevel).
    With().
    Timestamp().
    Logger()
 logger.Info().Msg("this is message")
Copy after login

`

I got the result:

<块引用>

[2024-01-16T13:24:05 07:00] [Message] - This is the message

Is it possible to change the position so that the result looks like:

<块引用>

[ INFO ] [2024-01-16T13:24:05 07:00] - This is the message

Thanks.

Workaround

You can use PartsOrder to do this; you will also need to adjust the formatter so that - is in the correct location ( playground ).

func main() {
    consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true, TimeFormat: time.RFC3339}

    consoleWriter.FormatLevel = func(i interface{}) string {
        return strings.ToUpper(fmt.Sprintf("[ %-6s]", i))
    }
    //consoleWriter.FormatTimestamp = func(i interface{}) string {
    //  return strings.ToUpper(fmt.Sprintf("[%s] -", i))
    //}
    consoleWriter.TimeFormat = "[" + time.RFC3339 + "] - "
    consoleWriter.PartsOrder = []string{
        zerolog.LevelFieldName,
        zerolog.TimestampFieldName,
        zerolog.CallerFieldName,
        zerolog.MessageFieldName,
    }
    logger := zerolog.New(consoleWriter).Level(zerolog.TraceLevel).
        With().
        Timestamp().
        Logger()
    logger.Info().Msg("this is message")
}
Copy after login

Output:

[ INFO  ] [2024-01-16T21:11:39+13:00] -  this is message
Copy after login

The above is the detailed content of Change location level and timestamp Zerolog golang. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!