Skip to content

Commit

Permalink
don't pre-create log files (not required anymore) (#2267)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mmetc committed Jun 9, 2023
1 parent 089eb92 commit 45b169b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
28 changes: 11 additions & 17 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -117,7 +119,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 {
Expand Down Expand Up @@ -163,15 +165,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
Expand Down
12 changes: 1 addition & 11 deletions pkg/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 45b169b

Please sign in to comment.