From 5b3200173eecc750b35b59737f89f2cbc3b8fb08 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 7 Jun 2023 12:58:35 +0200 Subject: [PATCH] don't pre-create log files (not required anymore) (#2267) The lumberjack package fixed the issue in natefinch/lumberjack#83 (tested with umask 002) and this code is now redundant since we updated the dependency to v2.2.1. --- pkg/apiserver/apiserver.go | 28 +++++++++++----------------- pkg/types/utils.go | 12 +----------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index c65100573..3facd0f6f 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -9,9 +9,18 @@ import ( "net" "net/http" "os" + "path/filepath" "strings" "time" + "github.com/gin-gonic/gin" + "github.com/go-co-op/gocron" + "github.com/golang-jwt/jwt/v4" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + "gopkg.in/natefinch/lumberjack.v2" + "gopkg.in/tomb.v2" + "github.com/crowdsecurity/go-cs-lib/pkg/trace" "github.com/crowdsecurity/crowdsec/pkg/apiclient" @@ -22,13 +31,6 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/fflag" "github.com/crowdsecurity/crowdsec/pkg/types" - "github.com/gin-gonic/gin" - "github.com/go-co-op/gocron" - "github.com/golang-jwt/jwt/v4" - "github.com/pkg/errors" - log "github.com/sirupsen/logrus" - "gopkg.in/natefinch/lumberjack.v2" - "gopkg.in/tomb.v2" ) var ( @@ -116,7 +118,7 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) { logFile := "" if config.LogMedia == "file" { - logFile = fmt.Sprintf("%s/crowdsec_api.log", config.LogDir) + logFile = filepath.Join(config.LogDir, "crowdsec_api.log") } if log.GetLevel() < log.DebugLevel { @@ -162,15 +164,7 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) { 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: _maxsize, //megabytes diff --git a/pkg/types/utils.go b/pkg/types/utils.go index caed6961e..42d3ba189 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -40,19 +40,9 @@ 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: fname, + Filename: filepath.Join(cfgFolder, "crowdsec.log"), MaxSize: _maxsize, MaxBackups: _maxfiles, MaxAge: _maxage,