Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type (
Encode string `xml:"encode,attr"`
FileMaxSize int64 `xml:"filemaxsize,attr"` //日志文件最大容量,单位为KB
FileName string `xml:"filename,attr"`
MaxBackups int `xml:"maxbackups,attr"` //保留备份文件数量,0表示不清理
}

UdpTargetConfig struct {
Expand Down
1 change: 1 addition & 0 deletions config/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ type JSONTargetConfig struct {
Encode string `yaml:"encode"`
FileName string `yaml:"fileName"`
FileMaxSize int64 `yaml:"fileMaxSize"`
MaxBackups int `yaml:"maxBackups"` // 保留备份文件数量,0 表示不清理
PrettyPrint *bool `yaml:"prettyPrint"`
}
3 changes: 3 additions & 0 deletions config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type fileTargetConfig struct {
Encode string `yaml:"encode"`
FileMaxSize int64 `yaml:"fileMaxSize"`
FileName string `yaml:"fileName"`
MaxBackups int `yaml:"maxBackups"` // 保留备份文件数量,0 表示不清理
}

type udpTargetConfig struct {
Expand Down Expand Up @@ -160,6 +161,7 @@ func LoadYamlConfig(configFile string) (*AppConfig, error) {
Encode: t.Encode,
FileMaxSize: t.FileMaxSize,
FileName: t.FileName,
MaxBackups: t.MaxBackups,
}
}

Expand Down Expand Up @@ -220,6 +222,7 @@ func LoadYamlConfig(configFile string) (*AppConfig, error) {
Encode: t.Encode,
FileName: t.FileName,
FileMaxSize: t.FileMaxSize,
MaxBackups: t.MaxBackups,
PrettyPrint: t.PrettyPrint,
}
}
Expand Down
2 changes: 1 addition & 1 deletion const/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package _const

const(
Version = "0.9.4"
Version = "0.10.1"
)

const (
Expand Down
51 changes: 51 additions & 0 deletions example/rotation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"
"time"

"github.com/devfeel/dotlog"
)

// 本示例展示日志轮转功能
// 运行方式: go run main.go
// 配置文件: rotation.yaml
func main() {
// 启动日志服务(使用 YAML 配置)
err := dotlog.StartLogService("rotation.yaml")
if err != nil {
fmt.Printf("Failed to start log service: %v\n", err)
return
}

// 获取带轮转的 FileLogger
fileLog := dotlog.GetLogger("FileLogger")

// 获取 JSON Logger
jsonLog := dotlog.GetLogger("JSONLogger")

fmt.Println("开始写入日志(测试轮转功能)...")
fmt.Println("每条日志后会打印当前文件大小,达到 FileMaxSize 后会自动轮转")
fmt.Println("按 Ctrl+C 停止")

// 写入大量日志来触发轮转
count := 0
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

for range ticker.C {
count++
msg := fmt.Sprintf("Test log message #%d - %s", count, time.Now().Format("15:04:05.000"))

// 写入文件日志
fileLog.Info(msg)

// 写入 JSON 日志
jsonLog.Info(msg)

// 每 100 条打印一次
if count%100 == 0 {
fmt.Printf("已写入 %d 条日志\n", count)
}
}
}
87 changes: 87 additions & 0 deletions example/rotation/rotation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# dotlog 日志轮转配置示例
# 演示 FileTarget 和 JSONTarget 的轮转功能

global:
isLog: true
chanSize: 1000
innerLogPath: "./logs/"
innerLogEncode: "utf-8"

# 自定义变量
variables:
- name: LogDir
value: "./logs/"

# 日志输出目标 - 演示轮转功能
targets:
# 文件输出 - 带轮转
file:
- name: FileLogger
isLog: true
layout: "{datetime} - {message}"
encode: "utf-8"
fileMaxSize: 10 # 10KB,测试时使用小值快速触发轮转
fileName: "./logs/rotation/app.log"
maxBackups: 3 # 保留 3 个备份文件

# JSON 输出 - 带轮转
json:
- name: JSONLogger
isLog: true
layout: "{datetime} - {message}"
encode: "utf-8"
fileName: "./logs/rotation/app.json"
fileMaxSize: 10 # 10KB
maxBackups: 3
prettyPrint: true

# 标准输出(无轮转)
fmt:
- name: StdoutLogger
isLog: true
layout: "[{level}] {datetime} - {message}"
encode: "utf-8"

# 日志记录器配置
loggers:
- name: FileLogger
isLog: true
layout: "{datetime} - {message}"
configMode: "file"
levels:
- level: trace
targets: "FileLogger"
isLog: true
- level: debug
targets: "FileLogger"
isLog: true
- level: info
targets: "FileLogger"
isLog: true
- level: warn
targets: "FileLogger"
isLog: true
- level: error
targets: "FileLogger"
isLog: true

- name: JSONLogger
isLog: true
layout: "{datetime} - {message}"
configMode: "json"
levels:
- level: info
targets: "JSONLogger"
isLog: true
- level: error
targets: "JSONLogger"
isLog: true

- name: StdoutLogger
isLog: true
layout: "[{level}] {datetime} - {message}"
configMode: "fmt"
levels:
- level: info
targets: "StdoutLogger"
isLog: true
Loading
Loading