parent
a17f150e5d
commit
a88848009a
3 changed files with 43 additions and 8 deletions
|
@ -136,12 +136,37 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) {
|
|||
|
||||
/*Configure logs*/
|
||||
if logFile != "" {
|
||||
_maxsize := 500
|
||||
if config.LogMaxSize != 0 {
|
||||
_maxsize = config.LogMaxSize
|
||||
}
|
||||
_maxfiles := 3
|
||||
if config.LogMaxFiles != 0 {
|
||||
_maxfiles = config.LogMaxFiles
|
||||
}
|
||||
_maxage := 28
|
||||
if config.LogMaxAge != 0 {
|
||||
_maxage = config.LogMaxAge
|
||||
}
|
||||
_compress := true
|
||||
if config.CompressLogs != nil {
|
||||
_compress = *config.CompressLogs
|
||||
}
|
||||
/*cf. https://github.com/natefinch/lumberjack/issues/82
|
||||
let's create the file beforehand w/ the right perms */
|
||||
// check if file exists
|
||||
_, err := os.Stat(logFile)
|
||||
// create file if not exists, purposefully ignore errors
|
||||
if os.IsNotExist(err) {
|
||||
file, _ := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0600)
|
||||
file.Close()
|
||||
}
|
||||
LogOutput := &lumberjack.Logger{
|
||||
Filename: logFile,
|
||||
MaxSize: 500, //megabytes
|
||||
MaxBackups: 3,
|
||||
MaxAge: 28, //days
|
||||
Compress: true, //disabled by default
|
||||
MaxSize: _maxsize, //megabytes
|
||||
MaxBackups: _maxfiles,
|
||||
MaxAge: _maxage, //days
|
||||
Compress: _compress, //disabled by default
|
||||
}
|
||||
clog.SetOutput(LogOutput)
|
||||
}
|
||||
|
|
|
@ -368,9 +368,9 @@ func TestLoggingErrorToFileConfig(t *testing.T) {
|
|||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
//check file content
|
||||
_, err = ioutil.ReadFile(expectedFile)
|
||||
if err == nil {
|
||||
t.Fatalf("file should be empty")
|
||||
x, err := ioutil.ReadFile(expectedFile)
|
||||
if err == nil && len(x) > 0 {
|
||||
t.Fatalf("file should be empty, got '%s'", x)
|
||||
}
|
||||
|
||||
os.Remove("./crowdsec.log")
|
||||
|
|
|
@ -42,9 +42,19 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
|
|||
if compress != nil {
|
||||
_compress = *compress
|
||||
}
|
||||
/*cf. https://github.com/natefinch/lumberjack/issues/82
|
||||
let's create the file beforehand w/ the right perms */
|
||||
fname := cfgFolder + "/crowdsec.log"
|
||||
// check if file exists
|
||||
_, err := os.Stat(fname)
|
||||
// create file if not exists, purposefully ignore errors
|
||||
if os.IsNotExist(err) {
|
||||
file, _ := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0600)
|
||||
file.Close()
|
||||
}
|
||||
|
||||
LogOutput = &lumberjack.Logger{
|
||||
Filename: cfgFolder + "/crowdsec.log",
|
||||
Filename: fname,
|
||||
MaxSize: _maxsize,
|
||||
MaxBackups: _maxfiles,
|
||||
MaxAge: _maxage,
|
||||
|
|
Loading…
Add table
Reference in a new issue