日誌

參考範例:dubbo-go-samples/logger

1. 日誌配置

dubbogo 3.0 預設使用 zap 日誌庫。如果未在配置文件中添加 logger 配置,則日誌將會打印到控制台。默認級別為 debug。您也可以在配置文件中配置日誌級別。您可以如下配置 zap-config 和 lumberjack-config 來自定義日誌輸出。

dubbo:
  logger:
    zap-config:
      level: debug # log level
      development: false
      disableCaller: false
      disableStacktrace: false
      encoding: "console"
      # zap encoder configuration
      encoderConfig:
        messageKey: "message"
        levelKey: "level"
        timeKey: "time"
        nameKey: "logger"
        callerKey: "caller"
        stacktraceKey: "stacktrace"
        lineEnding: ""
        levelEncoder: "capitalColor"
        timeEncoder: "iso8601"
        durationEncoder: "seconds"
        callerEncoder: "short"
        nameEncoder: ""
      outputPaths:
        - "stderr"
      errorOutputPaths:
        - "stderr"
    lumberjack-config:
       # Write the log file name
      filename: "logs.log"
      # The maximum size of each log file length, in MiB. Default 100MiB
      maxSize: 1
      # The maximum number of days to keep the log (only keep the log of the last few days)
      maxAge: 3
      # Only how many recent log files are kept to control the size of the total log of the program
      maxBackups: 5
      # Whether to use local time or not, UTC time is used by default
      localTime: true
      # Whether to compress the log file, compression method gzip
      compress: false

2. 日誌 API 和自定義日誌

日誌介面

type Logger interface {
Info(args...interface{})
Warn(args ... interface{})
Error(args...interface{})
Debug(args...interface{})
Fatal(args...interface{})

Infof(fmt string, args...interface{})
Warnf(fmt string, args...interface{})
Errorf(fmt string, args...interface{})
Debugf(fmt string, args...interface{})
Fatalf(fmt string, args ... interface{})
}

日誌 API

import "dubbo.apache.org/dubbo-go/v3/common/logger"


logger.SetLoggerLevel(warn) // Set the log level in the main function
logger.SetLogger(myLogger) // Set a custom logger in the main function
  • 日誌 API 不能在 Init 階段使用,否則可能會出現意外問題。

上次修改時間:2024 年 1 月 17 日:修正損壞的連結 (6651e217e73)