Skip to content

Commit

Permalink
refactor: prefer logrus.WithField over WithFields with a single param (
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jun 20, 2024
1 parent 4521a98 commit 659774f
Show file tree
Hide file tree
Showing 25 changed files with 57 additions and 150 deletions.
8 changes: 2 additions & 6 deletions cmd/crowdsec-cli/config_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func (cli *cliConfig) backupHub(dirPath string) error {
}

for _, itemType := range cwhub.ItemTypes {
clog := log.WithFields(log.Fields{
"type": itemType,
})
clog := log.WithField("type", itemType)

itemMap := hub.GetItemMap(itemType)
if itemMap == nil {
Expand All @@ -39,9 +37,7 @@ func (cli *cliConfig) backupHub(dirPath string) error {
upstreamParsers := []string{}

for k, v := range itemMap {
clog = clog.WithFields(log.Fields{
"file": v.Name,
})
clog = clog.WithField("file", v.Name)
if !v.State.Installed { // only backup installed ones
clog.Debugf("[%s]: not installed", k)
continue
Expand Down
4 changes: 1 addition & 3 deletions pkg/acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ func LoadAcquisitionFromDSN(dsn string, labels map[string]string, transformExpr
if err := types.ConfigureLogger(clog); err != nil {
return nil, fmt.Errorf("while configuring datasource logger: %w", err)
}
subLogger := clog.WithFields(log.Fields{
"type": dsn,
})
subLogger := clog.WithField("type", dsn)
uniqueId := uuid.NewString()
if transformExpr != "" {
vm, err := expr.Compile(transformExpr, exprhelpers.GetExprOptions(map[string]interface{}{"evt": &types.Event{}})...)
Expand Down
4 changes: 1 addition & 3 deletions pkg/acquisition/modules/appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ func (w *AppsecSource) Configure(yamlConfig []byte, logger *log.Entry, MetricsLe
runner := AppsecRunner{
inChan: w.InChan,
UUID: appsecRunnerUUID,
logger: w.logger.WithFields(log.Fields{
"runner_uuid": appsecRunnerUUID,
}),
logger: w.logger.WithField("runner_uuid", appsecRunnerUUID),
AppsecRuntime: &wrt,
Labels: w.config.Labels,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/appsec/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func loadAppSecEngine(test appsecRuleTest, t *testing.T) {
InChan := make(chan appsec.ParsedRequest)
OutChan := make(chan types.Event)

logger := log.WithFields(log.Fields{"test": test.name})
logger := log.WithField("test", test.name)

//build rules
for ridx, rule := range test.inband_rules {
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func (cw *CloudwatchSource) LogStreamManager(in chan LogStreamTailConfig, outCha
openedStreams.With(prometheus.Labels{"group": newStream.GroupName}).Inc()
}
newStream.t = tomb.Tomb{}
newStream.logger = cw.logger.WithFields(log.Fields{"stream": newStream.StreamName})
newStream.logger = cw.logger.WithField("stream", newStream.StreamName)
cw.logger.Debugf("starting tail of stream %s", newStream.StreamName)
newStream.t.Go(func() error {
return cw.TailLogStream(&newStream, outChan)
Expand Down
2 changes: 1 addition & 1 deletion pkg/acquisition/modules/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (d *DockerSource) DockerManager(in chan *ContainerConfig, deleteChan chan *
case newContainer := <-in:
if _, ok := d.runningContainerState[newContainer.ID]; !ok {
newContainer.t = &tomb.Tomb{}
newContainer.logger = d.logger.WithFields(log.Fields{"container_name": newContainer.Name})
newContainer.logger = d.logger.WithField("container_name", newContainer.Name)
newContainer.t.Go(func() error {
return d.TailDocker(newContainer, outChan, deleteChan)
})
Expand Down
24 changes: 6 additions & 18 deletions pkg/acquisition/modules/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ container_name:
},
}

subLogger := log.WithFields(log.Fields{
"type": "docker",
})
subLogger := log.WithField("type", "docker")

for _, test := range tests {
f := DockerSource{}
Expand Down Expand Up @@ -108,9 +106,7 @@ func TestConfigureDSN(t *testing.T) {
expectedErr: "",
},
}
subLogger := log.WithFields(log.Fields{
"type": "docker",
})
subLogger := log.WithField("type", "docker")

for _, test := range tests {
f := DockerSource{}
Expand Down Expand Up @@ -169,13 +165,9 @@ container_name_regexp:

if ts.expectedOutput != "" {
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
"type": "docker",
})
subLogger = logger.WithField("type", "docker")
} else {
subLogger = log.WithFields(log.Fields{
"type": "docker",
})
subLogger = log.WithField("type", "docker")
}

readLogs = false
Expand Down Expand Up @@ -310,14 +302,10 @@ func TestOneShot(t *testing.T) {

if ts.expectedOutput != "" {
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
"type": "docker",
})
subLogger = logger.WithField("type", "docker")
} else {
log.SetLevel(ts.logLevel)
subLogger = log.WithFields(log.Fields{
"type": "docker",
})
subLogger = log.WithField("type", "docker")
}

readLogs = false
Expand Down
20 changes: 5 additions & 15 deletions pkg/acquisition/modules/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ exclude_regexps: ["as[a-$d"]`,
},
}

subLogger := log.WithFields(log.Fields{
"type": "file",
})
subLogger := log.WithField("type", "file")

for _, tc := range tests {
tc := tc
Expand Down Expand Up @@ -91,9 +89,7 @@ func TestConfigureDSN(t *testing.T) {
},
}

subLogger := log.WithFields(log.Fields{
"type": "file",
})
subLogger := log.WithField("type", "file")

for _, tc := range tests {
tc := tc
Expand Down Expand Up @@ -211,9 +207,7 @@ filename: test_files/test_delete.log`,
logger, hook := test.NewNullLogger()
logger.SetLevel(tc.logLevel)

subLogger := logger.WithFields(log.Fields{
"type": "file",
})
subLogger := logger.WithField("type", "file")

tomb := tomb.Tomb{}
out := make(chan types.Event, 100)
Expand Down Expand Up @@ -372,9 +366,7 @@ force_inotify: true`, testPattern),
logger, hook := test.NewNullLogger()
logger.SetLevel(tc.logLevel)

subLogger := logger.WithFields(log.Fields{
"type": "file",
})
subLogger := logger.WithField("type", "file")

tomb := tomb.Tomb{}
out := make(chan types.Event)
Expand Down Expand Up @@ -451,9 +443,7 @@ func TestExclusion(t *testing.T) {
exclude_regexps: ["\\.gz$"]`
logger, hook := test.NewNullLogger()
// logger.SetLevel(ts.logLevel)
subLogger := logger.WithFields(log.Fields{
"type": "file",
})
subLogger := logger.WithField("type", "file")

f := fileacquisition.FileSource{}
if err := f.Configure([]byte(config), subLogger, configuration.METRICS_NONE); err != nil {
Expand Down
24 changes: 6 additions & 18 deletions pkg/acquisition/modules/journalctl/journalctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ journalctl_filter:
},
}

subLogger := log.WithFields(log.Fields{
"type": "journalctl",
})
subLogger := log.WithField("type", "journalctl")

for _, test := range tests {
f := JournalCtlSource{}
Expand Down Expand Up @@ -97,9 +95,7 @@ func TestConfigureDSN(t *testing.T) {
},
}

subLogger := log.WithFields(log.Fields{
"type": "journalctl",
})
subLogger := log.WithField("type", "journalctl")

for _, test := range tests {
f := JournalCtlSource{}
Expand Down Expand Up @@ -153,13 +149,9 @@ journalctl_filter:
if ts.expectedOutput != "" {
logger, hook = test.NewNullLogger()
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
"type": "journalctl",
})
subLogger = logger.WithField("type", "journalctl")
} else {
subLogger = log.WithFields(log.Fields{
"type": "journalctl",
})
subLogger = log.WithField("type", "journalctl")
}

tomb := tomb.Tomb{}
Expand Down Expand Up @@ -227,13 +219,9 @@ journalctl_filter:
if ts.expectedOutput != "" {
logger, hook = test.NewNullLogger()
logger.SetLevel(ts.logLevel)
subLogger = logger.WithFields(log.Fields{
"type": "journalctl",
})
subLogger = logger.WithField("type", "journalctl")
} else {
subLogger = log.WithFields(log.Fields{
"type": "journalctl",
})
subLogger = log.WithField("type", "journalctl")
}

tomb := tomb.Tomb{}
Expand Down
8 changes: 2 additions & 6 deletions pkg/acquisition/modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ func TestStreamingAcquisition(t *testing.T) {
},
}

subLogger := log.WithFields(log.Fields{
"type": "kafka",
})
subLogger := log.WithField("type", "kafka")

createTopic("crowdsecplaintext", "localhost:9092")

Expand Down Expand Up @@ -222,9 +220,7 @@ func TestStreamingAcquisitionWithSSL(t *testing.T) {
},
}

subLogger := log.WithFields(log.Fields{
"type": "kafka",
})
subLogger := log.WithField("type", "kafka")

createTopic("crowdsecssl", "localhost:9092")

Expand Down
8 changes: 4 additions & 4 deletions pkg/acquisition/modules/kinesis/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (k *KinesisSource) ParseAndPushRecords(records []*kinesis.Record, out chan
}

func (k *KinesisSource) ReadFromSubscription(reader kinesis.SubscribeToShardEventStreamReader, out chan types.Event, shardId string, streamName string) error {
logger := k.logger.WithFields(log.Fields{"shard_id": shardId})
logger := k.logger.WithField("shard_id", shardId)
//ghetto sync, kinesis allows to subscribe to a closed shard, which will make the goroutine exit immediately
//and we won't be able to start a new one if this is the first one started by the tomb
//TODO: look into parent shards to see if a shard is closed before starting to read it ?
Expand Down Expand Up @@ -397,7 +397,7 @@ func (k *KinesisSource) EnhancedRead(out chan types.Event, t *tomb.Tomb) error {
return fmt.Errorf("resource part of stream ARN %s does not start with stream/", k.Config.StreamARN)
}

k.logger = k.logger.WithFields(log.Fields{"stream": parsedARN.Resource[7:]})
k.logger = k.logger.WithField("stream", parsedARN.Resource[7:])
k.logger.Info("starting kinesis acquisition with enhanced fan-out")
err = k.DeregisterConsumer()
if err != nil {
Expand Down Expand Up @@ -439,7 +439,7 @@ func (k *KinesisSource) EnhancedRead(out chan types.Event, t *tomb.Tomb) error {
}

func (k *KinesisSource) ReadFromShard(out chan types.Event, shardId string) error {
logger := k.logger.WithFields(log.Fields{"shard": shardId})
logger := k.logger.WithField("shard", shardId)
logger.Debugf("Starting to read shard")
sharIt, err := k.kClient.GetShardIterator(&kinesis.GetShardIteratorInput{ShardId: aws.String(shardId),
StreamName: &k.Config.StreamName,
Expand Down Expand Up @@ -485,7 +485,7 @@ func (k *KinesisSource) ReadFromShard(out chan types.Event, shardId string) erro
}

func (k *KinesisSource) ReadFromStream(out chan types.Event, t *tomb.Tomb) error {
k.logger = k.logger.WithFields(log.Fields{"stream": k.Config.StreamName})
k.logger = k.logger.WithField("stream", k.Config.StreamName)
k.logger.Info("starting kinesis acquisition from shards")
for {
shards, err := k.kClient.ListShards(&kinesis.ListShardsInput{
Expand Down
20 changes: 5 additions & 15 deletions pkg/acquisition/modules/kinesis/kinesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ stream_arn: arn:aws:kinesis:eu-west-1:123456789012:stream/my-stream`,
},
}

subLogger := log.WithFields(log.Fields{
"type": "kinesis",
})
subLogger := log.WithField("type", "kinesis")
for _, test := range tests {
f := KinesisSource{}
err := f.Configure([]byte(test.config), subLogger, configuration.METRICS_NONE)
Expand Down Expand Up @@ -171,9 +169,7 @@ stream_name: stream-1-shard`,
for _, test := range tests {
f := KinesisSource{}
config := fmt.Sprintf(test.config, endpoint)
err := f.Configure([]byte(config), log.WithFields(log.Fields{
"type": "kinesis",
}), configuration.METRICS_NONE)
err := f.Configure([]byte(config), log.WithField("type", "kinesis"), configuration.METRICS_NONE)
if err != nil {
t.Fatalf("Error configuring source: %s", err)
}
Expand Down Expand Up @@ -217,9 +213,7 @@ stream_name: stream-2-shards`,
for _, test := range tests {
f := KinesisSource{}
config := fmt.Sprintf(test.config, endpoint)
err := f.Configure([]byte(config), log.WithFields(log.Fields{
"type": "kinesis",
}), configuration.METRICS_NONE)
err := f.Configure([]byte(config), log.WithField("type", "kinesis"), configuration.METRICS_NONE)
if err != nil {
t.Fatalf("Error configuring source: %s", err)
}
Expand Down Expand Up @@ -266,9 +260,7 @@ from_subscription: true`,
for _, test := range tests {
f := KinesisSource{}
config := fmt.Sprintf(test.config, endpoint)
err := f.Configure([]byte(config), log.WithFields(log.Fields{
"type": "kinesis",
}), configuration.METRICS_NONE)
err := f.Configure([]byte(config), log.WithField("type", "kinesis"), configuration.METRICS_NONE)
if err != nil {
t.Fatalf("Error configuring source: %s", err)
}
Expand Down Expand Up @@ -312,9 +304,7 @@ use_enhanced_fanout: true`,
for _, test := range tests {
f := KinesisSource{}
config := fmt.Sprintf(test.config, endpoint)
err := f.Configure([]byte(config), log.WithFields(log.Fields{
"type": "kinesis",
}))
err := f.Configure([]byte(config), log.WithField("type", "kinesis"))
if err != nil {
t.Fatalf("Error configuring source: %s", err)
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/acquisition/modules/kubernetesaudit/k8s_audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ webhook_path: /k8s-audit`,
},
}

subLogger := log.WithFields(log.Fields{
"type": "k8s-audit",
})
subLogger := log.WithField("type", "k8s-audit")

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -230,9 +228,7 @@ webhook_path: /k8s-audit`,
},
}

subLogger := log.WithFields(log.Fields{
"type": "k8s-audit",
})
subLogger := log.WithField("type", "k8s-audit")

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 659774f

Please sign in to comment.