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

2 Commits
v2.0.1 ... v2

Author SHA1 Message Date
comma
000a30dfa5 🎨优化推送文本格式到loki时,堆栈信息换一行 2025-10-14 16:36:01 +08:00
comma
28c0c31ff9 🆕新增一个配置项,调控堆栈打印级别 2025-10-14 16:32:03 +08:00
6 changed files with 62 additions and 31 deletions

View File

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

View File

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

View File

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

View File

@@ -3,6 +3,7 @@ package zap_logger
import (
"errors"
"os"
"strings"
_ "embed"
@@ -68,11 +69,27 @@ func NewZapLogger(filePath string, opts ...Option) error {
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(
zapcore.NewTee(cores...), // 开启的日志核心
zap.AddCaller(), // 启用调用者信息
zap.AddCallerSkip(1), // 调用者信息跳过
zap.AddStacktrace(zap.ErrorLevel), // 开启panic日志错误堆栈收集
zapcore.NewTee(cores...), // 开启的日志核心
zap.AddCaller(), // 启用调用者信息
zap.AddCallerSkip(1), // 调用者信息跳过
zap.AddStacktrace(stackLevel), // 开启panic日志错误堆栈收集
)
zap.ReplaceGlobals(logger)
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 {
return func(conf *Config) {
conf.File.Enable = enable

View File

@@ -81,6 +81,7 @@ func (lw *LokiWriter) Write(p []byte) (int, error) {
builder.WriteString(li.Msg)
builder.WriteString(" ")
if li.Stacktrace != nil {
builder.WriteString("\n")
builder.WriteString(li.Stacktrace.(string))
}