Allow to configure log rotation (#1130)
This commit is contained in:
parent
7126f8f0ff
commit
3105897f37
|
@ -295,7 +295,8 @@ func main() {
|
|||
log.Fatalf(err.Error())
|
||||
}
|
||||
// Configure logging
|
||||
if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel); err != nil {
|
||||
if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel,
|
||||
cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,8 @@ func reloadHandler(sig os.Signal, cConfig *csconfig.Config) error {
|
|||
log.Fatalf(err.Error())
|
||||
}
|
||||
// Configure logging
|
||||
if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel); err != nil {
|
||||
if err = types.SetDefaultLoggerConfig(cConfig.Common.LogMedia, cConfig.Common.LogDir, *cConfig.Common.LogLevel,
|
||||
cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles, cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs); err != nil {
|
||||
log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
|
|
|
@ -275,7 +275,7 @@ func TestLoggingDebugToFileConfig(t *testing.T) {
|
|||
os.Remove(expectedFile)
|
||||
|
||||
// Configure logging
|
||||
if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel); err != nil {
|
||||
if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs); err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
api, err := NewServer(&cfg)
|
||||
|
@ -337,7 +337,7 @@ func TestLoggingErrorToFileConfig(t *testing.T) {
|
|||
os.Remove(expectedFile)
|
||||
|
||||
// Configure logging
|
||||
if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel); err != nil {
|
||||
if err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs); err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
api, err := NewServer(&cfg)
|
||||
|
|
|
@ -88,6 +88,10 @@ type LocalApiServerCfg struct {
|
|||
Profiles []*ProfileCfg `yaml:"-"`
|
||||
LogLevel *log.Level `yaml:"log_level"`
|
||||
UseForwardedForHeaders bool `yaml:"use_forwarded_for_headers,omitempty"`
|
||||
CompressLogs *bool `yaml:"-"`
|
||||
LogMaxSize int `yaml:"-"`
|
||||
LogMaxAge int `yaml:"-"`
|
||||
LogMaxFiles int `yaml:"-"`
|
||||
}
|
||||
|
||||
type TLSCfg struct {
|
||||
|
@ -102,6 +106,11 @@ func (c *Config) LoadAPIServer() error {
|
|||
}
|
||||
c.API.Server.LogDir = c.Common.LogDir
|
||||
c.API.Server.LogMedia = c.Common.LogMedia
|
||||
c.API.Server.CompressLogs = c.Common.CompressLogs
|
||||
c.API.Server.LogMaxSize = c.Common.LogMaxSize
|
||||
c.API.Server.LogMaxAge = c.Common.LogMaxAge
|
||||
c.API.Server.LogMaxFiles = c.Common.LogMaxFiles
|
||||
|
||||
if err := c.API.Server.LoadProfiles(); err != nil {
|
||||
return errors.Wrap(err, "while loading profiles for LAPI")
|
||||
}
|
||||
|
|
|
@ -16,6 +16,10 @@ type CommonCfg struct {
|
|||
LogDir string `yaml:"log_dir,omitempty"` //if LogMedia = file
|
||||
LogLevel *log.Level `yaml:"log_level"`
|
||||
WorkingDir string `yaml:"working_dir,omitempty"` ///var/run
|
||||
CompressLogs *bool `yaml:"compress_logs,omitempty"`
|
||||
LogMaxSize int `yaml:"log_max_size,omitempty"`
|
||||
LogMaxAge int `yaml:"log_max_age,omitempty"`
|
||||
LogMaxFiles int `yaml:"log_max_files,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Config) LoadCommon() error {
|
||||
|
|
|
@ -22,16 +22,33 @@ var logFormatter log.Formatter
|
|||
var LogOutput *lumberjack.Logger //io.Writer
|
||||
var logLevel log.Level
|
||||
|
||||
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level) error {
|
||||
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool) error {
|
||||
|
||||
/*Configure logs*/
|
||||
if cfgMode == "file" {
|
||||
_maxsize := 500
|
||||
if maxSize != 0 {
|
||||
_maxsize = maxSize
|
||||
}
|
||||
_maxfiles := 3
|
||||
if maxFiles != 0 {
|
||||
_maxfiles = maxFiles
|
||||
}
|
||||
_maxage := 28
|
||||
if maxAge != 0 {
|
||||
_maxage = maxAge
|
||||
}
|
||||
_compress := true
|
||||
if compress != nil {
|
||||
_compress = *compress
|
||||
}
|
||||
|
||||
LogOutput = &lumberjack.Logger{
|
||||
Filename: cfgFolder + "/crowdsec.log",
|
||||
MaxSize: 500, //megabytes
|
||||
MaxBackups: 3,
|
||||
MaxAge: 28, //days
|
||||
Compress: true, //disabled by default
|
||||
MaxSize: _maxsize,
|
||||
MaxBackups: _maxfiles,
|
||||
MaxAge: _maxage,
|
||||
Compress: _compress,
|
||||
}
|
||||
log.SetOutput(LogOutput)
|
||||
} else if cfgMode != "stdout" {
|
||||
|
|
Loading…
Reference in a new issue