Home  >  Article  >  Backend Development  >  How to Separate Logs by Level to Stdout and Stderr using uber-go/zap?

How to Separate Logs by Level to Stdout and Stderr using uber-go/zap?

Linda Hamilton
Linda HamiltonOriginal
2024-10-26 08:06:02819browse

How to Separate Logs by Level to Stdout and Stderr using uber-go/zap?

Logging to stdout and stderr using uber-go/zap

Separating the logging of different log levels to stdout and stderr using uber-go/zap can be achieved by employing a tee core. A tee core combines multiple cores, allowing logs to be written to different destinations based on their level.

How to create a tee core

To create a tee core, you need to construct two individual cores: one for stdout and another for stderr. Each core includes an encoder to format the log messages and a syncer to write the logs to the appropriate device.

Example tee core

The following snippet demonstrates how to create a tee core that separates info logs to stdout and error/fatal logs to stderr:

<code class="go">package main

import (
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "os"
)

func main() {
    // Syncers for stdout and stderr
    stdoutSyncer := zapcore.Lock(os.Stdout)
    stderrSyncer := zapcore.Lock(os.Stderr)

    // Create two level enablers
    infoLevel := zap.LevelEnablerFunc(
        func(level zapcore.Level) bool {
            return level == zapcore.InfoLevel
        },
    )

    errorFatalLevel := zap.LevelEnablerFunc(
        func(level zapcore.Level) bool {
            return level == zapcore.ErrorLevel || level == zapcore.FatalLevel
        },
    )

    // Create two cores
    infoCore := zapcore.NewCore(
        zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
        stdoutSyncer,
        infoLevel,
    )

    errorFatalCore := zapcore.NewCore(
        zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
        stderrSyncer,
        errorFatalLevel,
    )

    // Combine the cores into a tee core
    teeCore := zapcore.NewTee(
        infoCore,
        errorFatalCore,
    )

    // Create a logger with the tee core
    logger := zap.New(
        teeCore,
    )

    // Log messages at different levels
    logger.Info("Info log")
    logger.Error("Error log")
}</code>

Output redirection

To verify the separation of log output, you can redirect stdout or stderr to /dev/null and observation the remaining output.

<code class="bash">$ go build main.go

$ ./main 2>/dev/null # Shows only stdout
{
    "level": "info",
    "ts": 1626900981.520349,
    "msg": "info log"
}

$ ./main 1>/dev/null # Shows only stderr
{
    "level": "error",
   "ts": 1626901025.056065,
    "msg": "error log"
}</code>

The above is the detailed content of How to Separate Logs by Level to Stdout and Stderr using uber-go/zap?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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