Files
waf-platform/EdgeCommon/pkg/serverconfigs/access_log_storage_file.go

67 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
const (
DefaultAccessLogRotateMaxSizeMB = 256
DefaultAccessLogRotateMaxBackups = 14
DefaultAccessLogRotateMaxAgeDays = 7
)
// AccessLogRotateConfig 文件轮转配置。
type AccessLogRotateConfig struct {
MaxSizeMB int `yaml:"maxSizeMB" json:"maxSizeMB"` // 单文件最大大小MB
MaxBackups int `yaml:"maxBackups" json:"maxBackups"` // 保留历史文件数
MaxAgeDays int `yaml:"maxAgeDays" json:"maxAgeDays"` // 保留天数
Compress *bool `yaml:"compress" json:"compress"` // 是否压缩历史文件
LocalTime *bool `yaml:"localTime" json:"localTime"` // 轮转时间使用本地时区
}
// AccessLogFileStorageConfig 文件存储配置
type AccessLogFileStorageConfig struct {
Path string `yaml:"path" json:"path"` // 文件路径,支持变量:${year|month|week|day|hour|minute|second}
AutoCreate bool `yaml:"autoCreate" json:"autoCreate"` // 是否自动创建目录
Rotate *AccessLogRotateConfig `yaml:"rotate" json:"rotate"` // 文件轮转配置
}
// NewDefaultAccessLogRotateConfig 默认轮转配置。
func NewDefaultAccessLogRotateConfig() *AccessLogRotateConfig {
compress := false
localTime := true
return &AccessLogRotateConfig{
MaxSizeMB: DefaultAccessLogRotateMaxSizeMB,
MaxBackups: DefaultAccessLogRotateMaxBackups,
MaxAgeDays: DefaultAccessLogRotateMaxAgeDays,
Compress: &compress,
LocalTime: &localTime,
}
}
// Normalize 归一化轮转配置,空值/非法值回退默认。
func (c *AccessLogRotateConfig) Normalize() *AccessLogRotateConfig {
defaultConfig := NewDefaultAccessLogRotateConfig()
if c == nil {
return defaultConfig
}
if c.MaxSizeMB > 0 {
defaultConfig.MaxSizeMB = c.MaxSizeMB
}
if c.MaxBackups > 0 {
defaultConfig.MaxBackups = c.MaxBackups
}
if c.MaxAgeDays > 0 {
defaultConfig.MaxAgeDays = c.MaxAgeDays
}
if c.Compress != nil {
v := *c.Compress
defaultConfig.Compress = &v
}
if c.LocalTime != nil {
v := *c.LocalTime
defaultConfig.LocalTime = &v
}
return defaultConfig
}