mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-12 12:55:53 +02:00
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*/
|
/*Configure logs*/
|
||||||
if logFile != "" {
|
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{
|
LogOutput := &lumberjack.Logger{
|
||||||
Filename: logFile,
|
Filename: logFile,
|
||||||
MaxSize: 500, //megabytes
|
MaxSize: _maxsize, //megabytes
|
||||||
MaxBackups: 3,
|
MaxBackups: _maxfiles,
|
||||||
MaxAge: 28, //days
|
MaxAge: _maxage, //days
|
||||||
Compress: true, //disabled by default
|
Compress: _compress, //disabled by default
|
||||||
}
|
}
|
||||||
clog.SetOutput(LogOutput)
|
clog.SetOutput(LogOutput)
|
||||||
}
|
}
|
||||||
|
|
|
@ -368,9 +368,9 @@ func TestLoggingErrorToFileConfig(t *testing.T) {
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
//check file content
|
//check file content
|
||||||
_, err = ioutil.ReadFile(expectedFile)
|
x, err := ioutil.ReadFile(expectedFile)
|
||||||
if err == nil {
|
if err == nil && len(x) > 0 {
|
||||||
t.Fatalf("file should be empty")
|
t.Fatalf("file should be empty, got '%s'", x)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove("./crowdsec.log")
|
os.Remove("./crowdsec.log")
|
||||||
|
|
|
@ -42,9 +42,19 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
|
||||||
if compress != nil {
|
if compress != nil {
|
||||||
_compress = *compress
|
_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{
|
LogOutput = &lumberjack.Logger{
|
||||||
Filename: cfgFolder + "/crowdsec.log",
|
Filename: fname,
|
||||||
MaxSize: _maxsize,
|
MaxSize: _maxsize,
|
||||||
MaxBackups: _maxfiles,
|
MaxBackups: _maxfiles,
|
||||||
MaxAge: _maxage,
|
MaxAge: _maxage,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue