feat(log): support config rotate size and head room

Signed-off-by: setcy <asetcy@gmail.com>
This commit is contained in:
setcy 2023-11-11 18:57:51 +08:00 committed by 梁曟風
parent 0e4615bf3e
commit 7dfad1d6a0
3 changed files with 25 additions and 11 deletions

View File

@ -55,6 +55,8 @@ const (
ConfigKeyRole = "role"
ConfigKeyLogDir = "logDir"
ConfigKeyLogLevel = "logLevel"
ConfigKeyLogRotateSize = "logRotateSize"
ConfigKeyLogRotateHeadRoom = "logRotateHeadRoom"
ConfigKeyProfPort = "prof"
ConfigKeyWarnLogDir = "warnLogDir"
ConfigKeyBuffersTotalLimit = "buffersTotalLimit"
@ -161,6 +163,8 @@ func main() {
role := cfg.GetString(ConfigKeyRole)
logDir := cfg.GetString(ConfigKeyLogDir)
logLevel := cfg.GetString(ConfigKeyLogLevel)
logRotateSize := cfg.GetInt64(ConfigKeyLogRotateSize)
logRotateHeadRoom := cfg.GetInt64(ConfigKeyLogRotateHeadRoom)
profPort := cfg.GetString(ConfigKeyProfPort)
umpDatadir := cfg.GetString(ConfigKeyWarnLogDir)
buffersTotalLimit := cfg.GetInt64(ConfigKeyBuffersTotalLimit)
@ -222,8 +226,14 @@ func main() {
default:
level = log.ErrorLevel
}
_, err = log.InitLog(logDir, module, level, nil, logLeftSpaceLimit)
rotate := log.NewLogRotate()
if logRotateSize > 0 {
rotate.SetRotateSizeMb(logRotateSize)
}
if logRotateHeadRoom > 0 {
rotate.SetHeadRoomMb(logRotateHeadRoom)
}
_, err = log.InitLog(logDir, module, level, rotate, logLeftSpaceLimit)
if err != nil {
err = errors.NewErrorf("Fatal: failed to init log - %v", err)
fmt.Println(err)

View File

@ -316,13 +316,17 @@ func InitLog(dir, module string, level Level, rotate *LogRotate, logLeftSpaceLim
}
}
_ = os.Chmod(dir, 0755)
fs := syscall.Statfs_t{}
if err := syscall.Statfs(dir, &fs); err != nil {
return nil, fmt.Errorf("[InitLog] stats disk space: %s", err.Error())
}
if rotate == nil {
rotate = NewLogRotate()
fs := syscall.Statfs_t{}
if err := syscall.Statfs(dir, &fs); err != nil {
return nil, fmt.Errorf("[InitLog] stats disk space: %s",
err.Error())
}
}
if rotate.headRoom == 0 {
var minLogLeftSpaceLimit float64
if float64(fs.Bavail*uint64(fs.Bsize)) < float64(fs.Blocks*uint64(fs.Bsize))*DefaultHeadRatio {
minLogLeftSpaceLimit = float64(fs.Bavail*uint64(fs.Bsize)) * DefaultHeadRatio / 1024 / 1024
@ -333,13 +337,16 @@ func InitLog(dir, module string, level Level, rotate *LogRotate, logLeftSpaceLim
minLogLeftSpaceLimit = math.Max(minLogLeftSpaceLimit, float64(logLeftSpaceLimit))
rotate.SetHeadRoomMb(int64(math.Min(minLogLeftSpaceLimit, DefaultHeadRoom)))
}
if rotate.rotateSize == 0 {
minRotateSize := int64(fs.Bavail * uint64(fs.Bsize) / uint64(len(levelPrefixes)))
if minRotateSize < DefaultMinRotateSize {
minRotateSize = DefaultMinRotateSize
}
rotate.SetRotateSizeMb(int64(math.Min(float64(minRotateSize), float64(DefaultRotateSize))))
}
l.rotate = rotate
err = l.initLog(dir, module, level)
if err != nil {

View File

@ -34,10 +34,7 @@ type LogRotate struct {
// NewLogRotate returns a new LogRotate instance.
func NewLogRotate() *LogRotate {
return &LogRotate{
rotateSize: DefaultRotateSize,
headRoom: DefaultHeadRoom,
}
return &LogRotate{}
}
// SetRotateSizeMb sets the rotate size in terms of MB.