Go语言 如何使用配置文件优雅地配置Logger,同时支持日志循环

qyyhg6bp  于 2023-05-20  发布在  Go
关注(0)|答案(1)|浏览(188)

问题描述

  • 功能:Test1()是官方文档中推荐的原木旋切库gopkg.in/natefinch/lumberjack.v2
  • 功能:Test2()是一个基于官方文档中的基本配置,使用yaml读取配置的Logger。

在执行主函数之后,
在控制台输出中:
2023-05-15T08:49:16.555+0800 | INFO | logger construction succeeded:config from yaml | {"app": "jpz"}
在日志文件foo.log输出中:
{"level":"info","ts":1684111756.5545945,"msg":"logger construction succeeded:lumberjack.Logger"}
这两块木头肯定不一样。

我目前的需求:

1.两者都支持使用配置文件config_log_zap.yaml使所有配置生效,并让lumberjack完成日志轮换和拆分工作。
1.控制台和日志文件的输出应该是相同的,这样我就可以通过配置文件快速应用所需的内容。之所以需要控制台和日志文件,是因为我需要在开发过程中关注并记录过去的输出消息。
在控制台输出中:
2023-05-15T08:49:16.555+0800 | INFO | logger construction succeeded:config from yaml | {"app": "jpz"}
在日志文件foo.log输出中:
2023-05-15T08:49:16.555+0800 | INFO | logger construction succeeded:config from yaml | {"app": "jpz"}
1.如何将Test1()Test2()合并为一个函数Test0()以满足上述两个要求?
请给予我一些帮助,我已经研究了很长时间了。
main.go

package main

import (
    "gopkg.in/yaml.v3"
    "os"

    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    "gopkg.in/natefinch/lumberjack.v2"
)

func Test1() {
    // lumberjack.Logger is already safe for concurrent use, so we don't need to
    // lock it.
    w := zapcore.AddSync(&lumberjack.Logger{
        Filename:   "./foo.log",
        MaxSize:    500, // megabytes
        MaxBackups: 3,
        MaxAge:     28, // days
    })
    core := zapcore.NewCore(
        zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
        w,
        zap.InfoLevel,
    )
    logger := zap.New(core)
    logger.Info("logger construction succeeded:lumberjack.Logger")
}

func Test2() {
    var cfg zap.Config
    yamlFile, _ := os.ReadFile("./config_log_zap.yaml")
    if err := yaml.Unmarshal(yamlFile, &cfg); err != nil {
        panic(err)
    }

    logger := zap.Must(cfg.Build())
    defer logger.Sync()

    logger.Info("logger construction succeeded:config from yaml")
}

func main() {
    Test1()
    Test2()
}

config_log_zap.yaml

# For the full description for the configuration, see
# https://github.com/uber-go/zap/blob/382e2511e51cda8afde24f9e6e741f934308edfa/config.go#L58-L94
level: 'debug'
development: true
disableCaller: true
disableStacktrace: false
sampling:
  initial: 100
  thereafter: 100
encoding: 'console'
encoderConfig:
  messageKey: 'msg'
  levelKey: 'level'
  timeKey: 'ts'
  nameKey: 'logger'
  callerKey: 'caller'
  functionKey: 'function'
  stacktraceKey: 'stacktrace'
  skipLineEnding: false
  lineEnding: "\n"
  levelEncoder: 'capital'
  timeEncoder: 'iso8601'
  durationEncoder: 'string'
  callerEncoder: 'full'
  nameEncoder: 'full'
  consoleSeparator: ' | '
outputPaths:
  - 'stdout'
  - './foo.log'
errorOutputPaths:
  - 'stderr'
  - './error_logs'
initialFields:
  app: 'jpz'
6jygbczu

6jygbczu1#

使用zap.RegisterSink将lumberjack记录器注册为新的接收器:

package main

import (
    "net/url"
    "os"
    "strconv"
    "strings"

    "gopkg.in/yaml.v3"

    "go.uber.org/zap"
    "gopkg.in/natefinch/lumberjack.v2"
)

type lumberjackSink struct {
    lumberjack.Logger
}

func (l *lumberjackSink) Sync() error {
    return nil
}

func parseNumber(s string, fallback int) int {
    v, err := strconv.Atoi(s)
    if err == nil {
        return v
    }
    return fallback
}

func Test0() {
    if err := zap.RegisterSink("lumberjack", func(u *url.URL) (zap.Sink, error) {
        // Read parameters from URL:
        // lumberjack://localhost/foo.log?maxSize=500&maxBackups=3&maxAge=28
        filename := strings.TrimLeft(u.Path, "/")
        if filename == "" {
            filename = "foo.log"
        }
        q := u.Query()
        l := &lumberjackSink{
            Logger: lumberjack.Logger{
                Filename:   filename,
                MaxSize:    parseNumber(q.Get("maxSize"), 500),
                MaxBackups: parseNumber(q.Get("maxBackups"), 3),
                MaxAge:     parseNumber(q.Get("maxAge"), 28),
            },
        }
        return l, nil
    }); err != nil {
        panic(err)
    }

    var cfg zap.Config
    yamlFile, _ := os.ReadFile("./config_log_zap.yaml")
    if err := yaml.Unmarshal(yamlFile, &cfg); err != nil {
        panic(err)
    }

    logger := zap.Must(cfg.Build())
    defer logger.Sync()

    logger.Info("logger construction succeeded:config from yaml")
}

func main() {
    Test0()
}

然后修改配置文件,像这样设置outputPaths

outputPaths:
  - stdout
  - lumberjack://localhost/foo.log?maxSize=500&maxBackups=3&maxAge=28

相关问题