configure logging earlier

This commit is contained in:
Marco Mariani 2023-01-17 12:12:46 +01:00 committed by mmetc
parent 91b0f8fee1
commit 47dbfa770d
5 changed files with 21 additions and 13 deletions

View file

@ -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 != "" {

View file

@ -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

View file

@ -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}"

View file

@ -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"

View file

@ -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)'
}