Skip to content

Commit

Permalink
feat: logx support logs rotation based on size limitation. (zeromicro…
Browse files Browse the repository at this point in the history
…#1652)

implementation of zeromicro#1652

Totally compatible with the old logx.LogConf. No effect if users do not change their options.
  • Loading branch information
SgtDaJim committed Jul 20, 2022
1 parent 2567881 commit 89dd14a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
20 changes: 3 additions & 17 deletions core/logx/config.go
@@ -1,7 +1,5 @@
package logx

import "fmt"

type LogRotationRuleType int

const (
Expand Down Expand Up @@ -29,19 +27,7 @@ type LogConf struct {
// Only take effect when RotationRuleType is `LogRotationRuleTypeSizeLimit`
MaxSize int `json:",default=0"`
// RotationRuleType represents the type of log rotation rule. Default is DailyRotateRule.
RotationRuleType LogRotationRuleType `json:",default=LogRotationRuleTypeDaily,options=[LogRotationRuleTypeDaily,LogRotationRuleTypeSizeLimit]"`
}

func (t *LogRotationRuleType) UnmarshalText(b []byte) error {
switch string(b) {
case "LogRotationRuleTypeDaily":
*t = LogRotationRuleTypeDaily
case "LogRotationRuleTypeSizeLimit":
*t = LogRotationRuleTypeSizeLimit
case "":
*t = LogRotationRuleTypeDaily
default:
return fmt.Errorf("invalid option of RotationRuleType: %v", b)
}
return nil
// 0: LogRotationRuleTypeDaily
// 1: LogRotationRuleTypeSizeLimit
RotationRuleType LogRotationRuleType `json:",default=0,options=[0,1]"`
}
8 changes: 4 additions & 4 deletions core/logx/readme-cn.md
Expand Up @@ -19,7 +19,7 @@ type LogConf struct {
StackCooldownMillis int `json:",default=100"`
MaxBackups int `json:",default=0"`
MaxSize int `json:",default=0"`
RotationRuleType LogRotationRuleType `json:",default=LogRotationRuleTypeDaily,options=[LogRotationRuleTypeDaily,LogRotationRuleTypeSizeLimit]"`
RotationRuleType LogRotationRuleType `json:",default=0,options=[0,1]"`
}
```

Expand All @@ -42,9 +42,9 @@ type LogConf struct {
- `StackCooldownMillis`:多少毫秒后再次写入堆栈跟踪。用来避免堆栈跟踪日志过多
- `MaxBackups`: 多少个日志文件备份将被保存。0代表所有备份都被保存。当`RotationRuleType`被设置为`LogRotationRuleTypeSizeLimit`时才会起作用。注意:`KeepDays`选项的优先级会比`MaxBackups`高,即使`MaxBackups`被设置为0,当达到`KeepDays`上限时备份文件同样会被删除。
- `MaxSize`: 当前被写入的日志文件最大可占用多少空间。0代表没有上限。单位为`MB`。当`RotationRuleType`被设置为`LogRotationRuleTypeSizeLimit`时才会起作用。
- `RotationRuleType`: 日志轮转策略类型。默认为`LogRotationRuleTypeDaily`(按天轮转)。
- `LogRotationRuleTypeDaily`: 按天轮转。
- `LogRotationRuleTypeSizeLimit`: 按日志大小轮转。
- `RotationRuleType`: 日志轮转策略类型。默认为`LogRotationRuleTypeDaily`(按天轮转)(整形数值0)
- `LogRotationRuleTypeDaily`(整形数值0): 按天轮转。
- `LogRotationRuleTypeSizeLimit`(整形数值1): 按日志大小轮转。


## 打印日志方法
Expand Down
8 changes: 4 additions & 4 deletions core/logx/readme.md
Expand Up @@ -19,7 +19,7 @@ type LogConf struct {
StackCooldownMillis int `json:",default=100"`
MaxBackups int `json:",default=0"`
MaxSize int `json:",default=0"`
RotationRuleType LogRotationRuleType `json:",default=LogRotationRuleTypeDaily,options=[LogRotationRuleTypeDaily,LogRotationRuleTypeSizeLimit]"`
RotationRuleType LogRotationRuleType `json:",default=0,options=[0,1]"`
}
```

Expand All @@ -42,9 +42,9 @@ type LogConf struct {
- `StackCooldownMillis`: how many milliseconds to rewrite stacktrace again. It’s used to avoid stacktrace flooding.
- `MaxBackups`: represents how many backup log files will be kept. 0 means all files will be kept forever. Only take effect when RotationRuleType is `LogRotationRuleTypeSizeLimit`. NOTE: the level of option `KeepDays` will be higher. Even thougth `MaxBackups` sets 0, log files will still be removed if the `KeepDays` limitation is reached.
- `MaxSize`: represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`. Only take effect when RotationRuleType is `LogRotationRuleTypeSizeLimit`.
- `RotationRuleType`: represents the type of log rotation rule. Default is LogRotationRuleTypeDaily.
- `LogRotationRuleTypeDaily`: rotate the logs by day.
- `LogRotationRuleTypeSizeLimit`: rotate the logs by size of logs.
- `RotationRuleType`: represents the type of log rotation rule. Default is LogRotationRuleTypeDaily (int value 0).
- `LogRotationRuleTypeDaily` (int value 0): rotate the logs by day.
- `LogRotationRuleTypeSizeLimit` (int value 1): rotate the logs by size of logs.

## Logging methods

Expand Down

0 comments on commit 89dd14a

Please sign in to comment.