Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to configure log format #799 #2941

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a8477f7
make it possible to enable json log
Apr 14, 2024
fe0c8f3
fix
Apr 14, 2024
c8652aa
fix typo
Apr 14, 2024
9a50396
fix typo
Apr 14, 2024
a72f223
fix typo
Apr 14, 2024
d98a569
fix typo
Apr 14, 2024
11b1e23
fix typo
Apr 14, 2024
9674cc1
fix typo
Apr 14, 2024
194ac7a
Add error handling
Apr 14, 2024
bf179f4
Add log_format to default config
Apr 14, 2024
cb6e59f
Fix syntax error in if statement
Apr 14, 2024
2355a93
Fix typo
Apr 14, 2024
d7022a2
Fix typo
Apr 14, 2024
99606b7
Fix some typos and change naming from native to text, makes more sense
Apr 15, 2024
ea83066
Set same timestamp format for json logging
Apr 15, 2024
2697c75
Fix formatting
Apr 15, 2024
e44b4cc
Move in if statement under previous
Apr 15, 2024
27d4311
Fix some formatting that got messed up
Apr 15, 2024
b5a3e1c
Default to text formatter, if log_format is not configured.
Apr 15, 2024
96b8408
defining logFormatter outside if statement so that log.SetFormatter(l…
Apr 15, 2024
1ee0358
Add variables that were undefined
Apr 15, 2024
c7cdb77
Argument were missing when calling SetDefaultLoggerConfig function
Apr 15, 2024
65dcc58
Fix order of arguments passed
Apr 15, 2024
8ad6220
Fix order of arguments passed
Apr 15, 2024
8ed607e
Fix typo
Apr 15, 2024
3c6e086
Merge branch 'master' into fix_799
mmetc May 7, 2024
babb74c
Implicit log_format = "text"
mmetc May 7, 2024
c05c46d
functional test
mmetc May 7, 2024
1ace3f1
ignore log_format in FatalHook
mmetc May 7, 2024
5cfc3c5
Merge branch 'master' into fix_799
buixor May 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/crowdsec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
cConfig.Common.LogDir, *cConfig.Common.LogLevel,
cConfig.Common.LogMaxSize, cConfig.Common.LogMaxFiles,
cConfig.Common.LogMaxAge, cConfig.Common.CompressLogs,
cConfig.Common.LogFormat,
cConfig.Common.ForceColorLogs); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ common:
daemonize: true
log_media: file
log_level: info
log_format: text
log_dir: /var/log/
log_max_size: 20
compress_logs: true
Expand All @@ -27,7 +28,7 @@ db_config:
type: sqlite
db_path: /var/lib/crowdsec/data/crowdsec.db
#max_open_conns: 100
#user:
#user:
#password:
#db_name:
#host:
Expand Down
3 changes: 2 additions & 1 deletion config/config_win.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ common:
daemonize: false
log_media: file
log_level: info
log_format: text
log_dir: C:\ProgramData\CrowdSec\log\
config_paths:
config_dir: C:\ProgramData\CrowdSec\config\
Expand All @@ -21,7 +22,7 @@ db_config:
log_level: info
type: sqlite
db_path: C:\ProgramData\CrowdSec\data\crowdsec.db
#user:
#user:
#password:
#db_name:
#host:
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
cfg.LogLevel = ptr.Of(log.DebugLevel)

// Configure logging
err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false)
err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, cfg.LogFormat, false)

Check failure on line 347 in pkg/apiserver/apiserver_test.go

View workflow job for this annotation

GitHub Actions / Build + tests

cfg.LogFormat undefined (type csconfig.LocalApiServerCfg has no field or method LogFormat)
require.NoError(t, err)

api, err := NewServer(&cfg)
Expand Down Expand Up @@ -394,7 +394,7 @@
cfg.LogLevel = ptr.Of(log.ErrorLevel)

// Configure logging
err := types.SetDefaultLoggerConfig(cfg.LogMedia, cfg.LogDir, *cfg.LogLevel, cfg.LogMaxSize, cfg.LogMaxFiles, cfg.LogMaxAge, cfg.CompressLogs, false)

Check failure on line 397 in pkg/apiserver/apiserver_test.go

View workflow job for this annotation

GitHub Actions / Build + tests

not enough arguments in call to types.SetDefaultLoggerConfig
require.NoError(t, err)

api, err := NewServer(&cfg)
Expand Down
3 changes: 2 additions & 1 deletion pkg/csconfig/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type CommonCfg struct {
WorkingDir string `yaml:"working_dir,omitempty"` // TODO: This is just for backward compat. Remove this later
CompressLogs *bool `yaml:"compress_logs,omitempty"`
LogMaxSize int `yaml:"log_max_size,omitempty"`
LogMaxAge int `yaml:"log_max_age,omitempty"`
LogFormat string `yaml:"log_format,omitempty"`
LogMaxAge int `yaml:"log_max_age,omitempty"`
LogMaxFiles int `yaml:"log_max_files,omitempty"`
ForceColorLogs bool `yaml:"force_color_logs,omitempty"`
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var logFormatter log.Formatter
var LogOutput *lumberjack.Logger //io.Writer
var logLevel log.Level

func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, LogFormat string, forceColors bool) error {
/*Configure logs*/
if cfgMode == "file" {
_maxsize := 500
Expand All @@ -33,6 +33,14 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
if compress != nil {
_compress = *compress
}

if LogFormat == "json" {
logFormatter = &log.JSONFormatter{TimestampFormat: time.RFC3339}
} else if LogFormat == "text" {
logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}
} else {
return fmt.Errorf("log format '%s' unknown", LogFormat)
LaurenceJJones marked this conversation as resolved.
Show resolved Hide resolved
}

LogOutput = &lumberjack.Logger{
Filename: filepath.Join(cfgFolder, "crowdsec.log"),
Expand All @@ -47,7 +55,7 @@ func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level
}
logLevel = cfgLevel
log.SetLevel(logLevel)
logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}

log.SetFormatter(logFormatter)
return nil
}
Expand Down
Loading