Skip to content

Commit 1cda8e9

Browse files
committed
proper logging configuration support
1 parent 4f46d80 commit 1cda8e9

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

config.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strconv"
67
"strings"
78

89
"github.com/aws/aws-sdk-go/aws"
910
"github.com/crowdsecurity/crowdsec/pkg/types"
1011
log "github.com/sirupsen/logrus"
12+
"gopkg.in/natefinch/lumberjack.v2"
1113
"gopkg.in/yaml.v2"
1214
)
1315

@@ -20,6 +22,10 @@ type bouncerConfig struct {
2022
LogLevel log.Level `yaml:"log_level"`
2123
LogMedia string `yaml:"log_media"`
2224
LogDir string `yaml:"log_dir"`
25+
LogMaxSize int `yaml:"log_max_size"`
26+
LogMaxAge int `yaml:"log_max_age"`
27+
LogMaxFiles int `yaml:"log_max_backups"`
28+
CompressLogs *bool `yaml:"compress_logs"`
2329
WebACLConfig []AclConfig `yaml:"waf_config"`
2430
}
2531

@@ -40,6 +46,7 @@ func getConfigFromEnv(config *bouncerConfig) {
4046
var key string
4147
var value string
4248
var acl *AclConfig
49+
var err error
4350
acls := make(map[byte]*AclConfig, 0)
4451

4552
for _, env := range os.Environ() {
@@ -104,6 +111,26 @@ func getConfigFromEnv(config *bouncerConfig) {
104111
config.LogMedia = value
105112
case "BOUNCER_LOG_DIR":
106113
config.LogDir = value
114+
case "BOUNCER_LOG_MAX_SIZE":
115+
config.LogMaxSize, err = strconv.Atoi(value)
116+
if err != nil {
117+
log.Warnf("Invalid log max size from env: %s, using 40", value)
118+
config.LogMaxSize = 40
119+
}
120+
case "BOUNCER_LOG_MAX_AGE":
121+
config.LogMaxAge, err = strconv.Atoi(value)
122+
if err != nil {
123+
log.Warnf("Invalid log max age from env: %s, using 7", value)
124+
config.LogMaxAge = 7
125+
}
126+
case "BOUNCER_LOG_MAX_FILES":
127+
config.LogMaxFiles, err = strconv.Atoi(value)
128+
if err != nil {
129+
log.Warnf("Invalid log max files from env: %s, using 7", value)
130+
config.LogMaxFiles = 7
131+
}
132+
case "BOUNCER_COMPRESS_LOGS":
133+
config.CompressLogs = aws.Bool(value == "true")
107134
}
108135
}
109136
}
@@ -139,10 +166,41 @@ func newConfig(configPath string) (bouncerConfig, error) {
139166
config.LogLevel = log.InfoLevel
140167
}
141168

142-
if err := types.SetDefaultLoggerConfig(config.LogMedia, config.LogDir, config.LogLevel, 10, 2, 1, aws.Bool(true)); err != nil {
169+
if err := types.SetDefaultLoggerConfig(config.LogMedia, config.LogDir, config.LogLevel, config.LogMaxSize, config.LogMaxFiles, config.LogMaxAge, config.CompressLogs); err != nil {
143170
log.Fatal(err.Error())
144171
}
145172

173+
if config.LogMedia == "file" {
174+
if config.LogDir == "" {
175+
config.LogDir = "/var/log/"
176+
}
177+
_maxsize := 40
178+
if config.LogMaxSize != 0 {
179+
_maxsize = config.LogMaxSize
180+
}
181+
_maxfiles := 3
182+
if config.LogMaxFiles != 0 {
183+
_maxfiles = config.LogMaxFiles
184+
}
185+
_maxage := 30
186+
if config.LogMaxAge != 0 {
187+
_maxage = config.LogMaxAge
188+
}
189+
_compress := true
190+
if config.CompressLogs != nil {
191+
_compress = *config.CompressLogs
192+
}
193+
logOutput := &lumberjack.Logger{
194+
Filename: config.LogDir + "/crowdsec-aws-waf-bouncer.log",
195+
MaxSize: _maxsize,
196+
MaxBackups: _maxfiles,
197+
MaxAge: _maxage,
198+
Compress: _compress,
199+
}
200+
log.SetOutput(logOutput)
201+
log.SetFormatter(&log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true})
202+
}
203+
146204
if config.APIKey == "" {
147205
return bouncerConfig{}, fmt.Errorf("api_key is required")
148206
}

0 commit comments

Comments
 (0)