codecamp
Dubbo-go 3.0 自定义日志

Dubbo-go 3.0 自定义日志

Dubbo-go 3.0 自定义日志组件

参考samples dubbo-go-samples/logger

1. 日志配置

Dubbo-go 3.0 默认采用 zap 日志库,在配置文件中不添加 logger 配置,日志将会打印到控制默认级别为debug。您也可在配置文件中配置日志级别、, 可参照如下方式来配置zap-config和lumberjack-config,从而定制化日志输出。

dubbo:
  logger:
    zap-config:
      level: debug # 日志级别
      development: false
      disableCaller: false
      disableStacktrace: false
      encoding: "console"
      # zap encoder 配置
      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:
       # 写日志的文件名称
      filename: "logs.log"
      # 每个日志文件长度的最大大小,单位是 MiB。默认 100MiB
      maxSize: 1
      # 日志保留的最大天数(只保留最近多少天的日志)
      maxAge: 3
      # 只保留最近多少个日志文件,用于控制程序总日志的大小
      maxBackups: 5
      # 是否使用本地时间,默认使用 UTC 时间
      localTime: true
      # 是否压缩日志文件,压缩方法 gzip
      compress: false

2. 日志API 和 自定义日志

日志Interface

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) // 在 main 函数中设置日志级别
logger.SetLogger(myLogger)  // 在 main 函数中设置自定义logger
  • 日志API不可以在Init 阶段使用,否则可能会发生意料之外的问题。


Dubbo-go 3.0 应用级服务发现
Dubbo-go 泛化调用
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

介绍与示例

应用级服务发现

动态修改运行态配置项

参考手册

配置中心参考手册

元数据参考手册

API 参考手册

Kubernetes 生命周期对齐探针

在线运维命令参考手册

Telnet 命令参考手册

Maven 插件参考手册

性能优化

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }