Add possibility to configure log format #799 (#2941)

* make it possible to enable json log

* fix

* fix typo

* fix typo

* fix typo

* fix typo

* fix typo

* fix typo

* Add error handling

* Add log_format to default config

* Fix syntax error in if statement

* Fix typo

* Fix typo

* Fix some typos and change naming from native to text, makes more sense

* Set same timestamp format for json logging

* Fix formatting

* Move in if statement under previous

* Fix some formatting that got messed up

* Default to text formatter, if log_format is not configured.

* defining logFormatter outside if statement so that log.SetFormatter(logFormatter) is not undefined when function is called

* Add variables that were undefined

* Argument were missing when calling SetDefaultLoggerConfig function

* Fix order of arguments passed

* Fix order of arguments passed

* Fix typo

* Implicit log_format = "text"

* functional test

* ignore log_format in FatalHook

* make it possible to enable json log

* fix

* fix typo

* fix typo

* fix typo

* fix typo

* fix typo

* fix typo

* Add error handling

* Add log_format to default config

* Fix syntax error in if statement

* Fix typo

* Fix typo

* Fix some typos and change naming from native to text, makes more sense

* Set same timestamp format for json logging

* Fix formatting

* Move in if statement under previous

* Fix some formatting that got messed up

* Default to text formatter, if log_format is not configured.

* defining logFormatter outside if statement so that log.SetFormatter(logFormatter) is not undefined when function is called

* Add variables that were undefined

* Argument were missing when calling SetDefaultLoggerConfig function

* Fix order of arguments passed

* Fix order of arguments passed

* Fix typo

* Implicit log_format = "text"

* functional test

* ignore log_format in FatalHook

* lint

* fix func test

* lint

* remove < > characters from log

---------

Co-authored-by: Victor Edvardsson <victor.edvardsson@loopia.se>
Co-authored-by: marco <marco@crowdsec.net>
Co-authored-by: Thibault "bui" Koechlin <thibault@crowdsec.net>
This commit is contained in:
victoredvardsson 2024-12-23 11:08:33 +01:00 committed by GitHub
parent 4748720a07
commit 466f39b880
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 107 additions and 18 deletions

View file

@ -67,6 +67,40 @@ teardown() {
refute_output
}
@test "crowdsec - log format" {
# fail early
config_disable_lapi
config_disable_agent
config_set '.common.log_media="stdout"'
config_set '.common.log_format=""'
rune -0 wait-for --err "you must run at least the API Server or crowdsec" "$CROWDSEC"
assert_stderr --partial 'level=fatal msg="you must run at least the API Server or crowdsec"'
config_set '.common.log_format="text"'
rune -0 wait-for --err "you must run at least the API Server or crowdsec" "$CROWDSEC"
assert_stderr --partial 'level=fatal msg="you must run at least the API Server or crowdsec"'
config_set '.common.log_format="json"'
rune -0 wait-for --err "you must run at least the API Server or crowdsec" "$CROWDSEC"
rune -0 jq -c 'select(.msg=="you must run at least the API Server or crowdsec") | .level' <(stderr | grep "^{")
assert_output '"fatal"'
# If log_media='file', a hook to stderr is added only for fatal messages,
# with a predefined formatter (level + msg, no timestamp, ignore log_format)
config_set '.common.log_media="file"'
config_set '.common.log_format="text"'
rune -0 wait-for --err "you must run at least the API Server or crowdsec" "$CROWDSEC"
assert_stderr --regexp 'FATAL.* you must run at least the API Server or crowdsec$'
config_set '.common.log_format="json"'
rune -0 wait-for --err "you must run at least the API Server or crowdsec" "$CROWDSEC"
assert_stderr --regexp 'FATAL.* you must run at least the API Server or crowdsec$'
}
@test "CS_LAPI_SECRET not strong enough" {
CS_LAPI_SECRET=foo rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "api server init: unable to run local API: controller init: CS_LAPI_SECRET not strong enough"

View file

@ -33,7 +33,7 @@ teardown() {
EOT
rune -1 "$CROWDSEC" -t
assert_stderr --partial "crowdsec init: while loading acquisition config: while configuring datasource of type file from $ACQUIS_DIR/file.yaml (position 0): cannot parse FileAcquisition configuration: yaml: unmarshal errors:\n line 6: cannot unmarshal !!seq into string"
assert_stderr --partial "crowdsec init: while loading acquisition config: while configuring datasource of type file from $ACQUIS_DIR/file.yaml (position 0): cannot parse FileAcquisition configuration: yaml: unmarshal errors:"
}
@test "datasource type detection" {