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

1 Commits

Author SHA1 Message Date
comma
28c0c31ff9 🆕新增一个配置项,调控堆栈打印级别 2025-10-14 16:32:03 +08:00
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 # 启用文件输出,默认关闭
@@ -85,9 +86,10 @@ 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)
| 字段名 | 类型 | 说明 | 默认值 | | 字段名 | 类型 | 说明 | 默认值 |
@@ -150,9 +152,10 @@ func main() {
完整选项列表: 完整选项列表:
| 函数名 | 说明 | 参数类型/示例 | | 函数名 | 说明 | 参数类型/示例 |
|----------------------------|-------------------------------|--------------------------------| |------------------------|-------------|----------------------------------------------------|
| WithEncoder | 设置全局编码器 | JsonEncoder/ConsoleEncoder | | WithEncoder | 设置全局编码器 | JsonEncoder/ConsoleEncoder |
| WithLevel | 设置全局日志级别 | "debug", "info", "warn"等 | | WithLevel | 设置全局日志级别 | "debug", "info", "warn"等 |
| WithStackLevel | 设置堆栈打印级别 | "debug", "info", "warn", "error", "panic", "fatal" |
| WithEnableFile | 启用/禁用文件输出 | true/false | | WithEnableFile | 启用/禁用文件输出 | true/false |
| WithFilename | 设置日志文件名 | "app.log" | | WithFilename | 设置日志文件名 | "app.log" |
| WithFileMaxSize | 设置文件最大尺寸(MB) | 20 | | WithFileMaxSize | 设置文件最大尺寸(MB) | 20 |
@@ -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

@@ -10,6 +10,7 @@ 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