浏览代码

configure logging earlier

Marco Mariani 2 年之前
父节点
当前提交
47dbfa770d
共有 5 个文件被更改,包括 21 次插入13 次删除
  1. 13 9
      cmd/crowdsec/main.go
  2. 1 4
      pkg/csconfig/crowdsec_service.go
  3. 1 0
      tests/bats/02_nolapi.bats
  4. 1 0
      tests/bats/03_noagent.bats
  5. 5 0
      tests/lib/setup_file.sh

+ 13 - 9
cmd/crowdsec/main.go

@@ -211,6 +211,19 @@ func LoadConfig(cConfig *csconfig.Config) error {
 		return err
 	}
 
+	// Configure logging
+	if err := types.SetDefaultLoggerConfig(cConfig.Common.LogMedia,
+		cConfig.Common.LogDir, *cConfig.Common.LogLevel,
+		cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles,
+		cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs,
+		cConfig.Common.ForceColorLogs); err != nil {
+		return err
+	}
+
+	if err := csconfig.LoadFeatureFlagsFile(cConfig, log.StandardLogger()); err != nil {
+		return err
+	}
+
 	if !flags.DisableAgent {
 		if err := cConfig.LoadCrowdsec(); err != nil {
 			return err
@@ -256,15 +269,6 @@ func LoadConfig(cConfig *csconfig.Config) error {
 		cConfig.Common.Daemonize = false
 	}
 
-	// Configure logging
-	if err := types.SetDefaultLoggerConfig(cConfig.Common.LogMedia,
-		cConfig.Common.LogDir, *cConfig.Common.LogLevel,
-		cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles,
-		cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs,
-		cConfig.Common.ForceColorLogs); err != nil {
-		return err
-	}
-
 	// recap of the enabled feature flags, because logging
 	// was not enabled when we set them from envvars
 	if fflist := csconfig.ListFeatureFlags(); fflist != "" {

+ 1 - 4
pkg/csconfig/crowdsec_service.go

@@ -40,10 +40,7 @@ type CrowdsecServiceCfg struct {
 func (c *Config) LoadCrowdsec() error {
 	var err error
 
-	if err = LoadFeatureFlagsFile(c, log.StandardLogger()); err != nil {
-		return err
-	}
-
+	log.Warningf("c.Crowdsec: %+v", c.Crowdsec)
 	if c.Crowdsec == nil {
 		log.Warning("crowdsec agent is disabled")
 		c.DisableAgent = true

+ 1 - 0
tests/bats/02_nolapi.bats

@@ -35,6 +35,7 @@ teardown() {
 
 @test "crowdsec should not run without LAPI (no api.server in configuration file)" {
     config_disable_lapi
+    config_log_stderr
     # really needs 4 secs on slow boxes
     run -1 --separate-stderr timeout 4s "${CROWDSEC}"
 

+ 1 - 0
tests/bats/03_noagent.bats

@@ -33,6 +33,7 @@ teardown() {
 
 @test "no agent: crowdsec LAPI should run (no crowdsec_service in configuration file)" {
     config_disable_agent
+    config_log_stderr
     run -124 --separate-stderr timeout 2s "${CROWDSEC}"
 
     assert_stderr --partial "crowdsec agent is disabled"

+ 5 - 0
tests/lib/setup_file.sh

@@ -71,6 +71,11 @@ config_disable_agent() {
 }
 export -f config_disable_agent
 
+config_log_stderr() {
+    config_set '.common.log_media="stdout"'
+}
+export -f config_log_stderr
+
 config_disable_lapi() {
     config_set 'del(.api.server)'
 }