Browse Source

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.
mmetc 2 years ago
parent
commit
5b3200173e
2 changed files with 12 additions and 28 deletions
  1. 11 17
      pkg/apiserver/apiserver.go
  2. 1 11
      pkg/types/utils.go

+ 11 - 17
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

+ 1 - 11
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,