2
0
mirror of https://gitee.ltd/lxh/logger.git synced 2025-10-23 14:56:16 +08:00

🆕新增一个配置项,调控堆栈打印级别

This commit is contained in:
comma
2025-10-14 16:32:03 +08:00
parent b82db96722
commit 28c0c31ff9
5 changed files with 61 additions and 31 deletions

View File

@@ -49,6 +49,7 @@ func main() {
logger: logger:
encoder: "json" # 全局编码器 (json/console) encoder: "json" # 全局编码器 (json/console)
level: "info" # 全局日志级别 level: "info" # 全局日志级别
stack_level: "panic"
file: file:
enable: true # 启用文件输出,默认关闭 enable: true # 启用文件输出,默认关闭
@@ -84,10 +85,11 @@ if err := zap_logger.NewZapLogger("logger.yaml"); err != nil {
## 配置说明 ## 配置说明
### 全局配置 (logger) ### 全局配置 (logger)
| 字段名 | 类型 | 说明 | 可选值 | 默认值 | | 字段名 | 类型 | 说明 | 可选值 | 默认值 |
|----------|--------|-------------------------------|------------------------|-----------| |-------------|--------|---------|------------------------|-----------|
| encoder | string | 全局日志编码器 | "json", "console" | "console" | | encoder | string | 全局日志编码器 | "json", "console" | "console" |
| level | string | 全局日志级别 | "debug", "info", "warn", "error", "dpanic", "panic", "fatal" | "info" | | level | string | 全局日志级别 | "debug", "info", "warn", "error", "dpanic", "panic", "fatal" | "info" |
| stack_level | string | 堆栈打印级别 | "info", "warn", "error", "dpanic", "panic", "fatal" | "panic" |
### 文件输出配置 (file) ### 文件输出配置 (file)
| 字段名 | 类型 | 说明 | 默认值 | | 字段名 | 类型 | 说明 | 默认值 |
@@ -149,26 +151,27 @@ func main() {
完整选项列表: 完整选项列表:
| 函数名 | 说明 | 参数类型/示例 | | 函数名 | 说明 | 参数类型/示例 |
|----------------------------|-------------------------------|--------------------------------| |------------------------|-------------|----------------------------------------------------|
| WithEncoder | 设置全局编码器 | JsonEncoder/ConsoleEncoder | | WithEncoder | 设置全局编码器 | JsonEncoder/ConsoleEncoder |
| WithLevel | 设置全局日志级别 | "debug", "info", "warn"等 | | WithLevel | 设置全局日志级别 | "debug", "info", "warn"等 |
| WithEnableFile | 启用/禁用文件输出 | true/false | | WithStackLevel | 设置堆栈打印级别 | "debug", "info", "warn", "error", "panic", "fatal" |
| WithFilename | 设置日志文件名 | "app.log" | | WithEnableFile | 启用/禁用文件输出 | true/false |
| WithFileMaxSize | 设置文件最大尺寸(MB) | 20 | | WithFilename | 设置日志文件名 | "app.log" |
| WithFileMaxAge | 设置日志保留天数 | 15 | | WithFileMaxSize | 设置文件最大尺寸(MB) | 20 |
| WithFileMaxBackups | 设置最大备份数量 | 10 | | WithFileMaxAge | 设置日志保留天数 | 15 |
| WithFileLocaltime | 备份文件使用本地时间 | true/false | | WithFileMaxBackups | 设置最大备份数量 | 10 |
| WithFileCompress | 启用/禁用备份压缩 | true/false | | WithFileLocaltime | 备份文件使用本地时间 | true/false |
| WithConsoleEnable | 启用/禁用控制台输出 | true/false | | WithFileCompress | 启用/禁用备份压缩 | true/false |
| WithConsoleEnableColor | 启用/禁用控制台彩色输出 | true/false | | WithConsoleEnable | 启用/禁用控制台输出 | true/false |
| WithLokiEnable | 启用/禁用Loki输出 | true/false | | WithConsoleEnableColor | 启用/禁用控制台彩色输出 | true/false |
| WithLokiHost | 设置Loki服务主机 | "loki.example.com" | | WithLokiEnable | 启用/禁用Loki输出 | true/false |
| WithLokiPort | 设置Loki服务端口 | 3100 | | WithLokiHost | 设置Loki服务主机 | "loki.example.com" |
| WithLokiSource | 设置日志来源标识 | "payment-service" | | WithLokiPort | 设置Loki服务端口 | 3100 |
| WithLokiService | 设置服务名称标签 | "api" | | WithLokiSource | 设置日志来源标识 | "payment-service" |
| WithLokiJob | 设置务名称标签 | "backend" | | WithLokiService | 设置务名称标签 | "api" |
| WithLokiEnvironment | 设置环境标识标签 | "production" | | WithLokiJob | 设置任务名称标签 | "backend" |
| WithLokiEnvironment | 设置环境标识标签 | "production" |
## 高级使用示例 ## 高级使用示例
@@ -179,6 +182,7 @@ func main() {
logger: logger:
encoder: "json" encoder: "json"
level: "info" level: "info"
stack_level: "panic"
file: file:
enable: true enable: true

View File

@@ -8,8 +8,9 @@ type Config struct {
} }
type Logger struct { type Logger struct {
Encoder string `yaml:"encoder"` Encoder string `yaml:"encoder"`
Level string `yaml:"level"` Level string `yaml:"level"`
StackLevel string `yaml:"stack_level"`
} }
type File struct { type File struct {

View File

@@ -1,9 +1,11 @@
# 通用配置 # 通用配置
logger: logger:
# 编码方式 json 或者 console # 编码方式 json 或者 console
encoder: json encoder: console
# 日志级别 # 日志级别
level: info level: info
# 堆栈输出级别
stack_level: panic
# 文件形式 # 文件形式
file: file:

View File

@@ -3,6 +3,7 @@ package zap_logger
import ( import (
"errors" "errors"
"os" "os"
"strings"
_ "embed" _ "embed"
@@ -68,11 +69,27 @@ func NewZapLogger(filePath string, opts ...Option) error {
cores = append(cores, newConsoleLogger(config.Console).Init()) cores = append(cores, newConsoleLogger(config.Console).Init())
} }
var stackLevel zapcore.Level
switch strings.ToLower(config.Logger.StackLevel) {
case "info":
stackLevel = zap.InfoLevel
case "warn":
stackLevel = zap.WarnLevel
case "error":
stackLevel = zap.ErrorLevel
case "panic":
stackLevel = zap.DPanicLevel
case "fatal":
stackLevel = zap.FatalLevel
default:
stackLevel = zap.DPanicLevel
}
logger := zap.New( logger := zap.New(
zapcore.NewTee(cores...), // 开启的日志核心 zapcore.NewTee(cores...), // 开启的日志核心
zap.AddCaller(), // 启用调用者信息 zap.AddCaller(), // 启用调用者信息
zap.AddCallerSkip(1), // 调用者信息跳过 zap.AddCallerSkip(1), // 调用者信息跳过
zap.AddStacktrace(zap.ErrorLevel), // 开启panic日志错误堆栈收集 zap.AddStacktrace(stackLevel), // 开启panic日志错误堆栈收集
) )
zap.ReplaceGlobals(logger) zap.ReplaceGlobals(logger)
return nil return nil

View File

@@ -21,6 +21,12 @@ func WithLevel(level string) Option {
} }
} }
func WithStackLevel(level string) Option {
return func(conf *Config) {
conf.Logger.StackLevel = level
}
}
func WithEnableFile(enable bool) Option { func WithEnableFile(enable bool) Option {
return func(conf *Config) { return func(conf *Config) {
conf.File.Enable = enable conf.File.Enable = enable