From 0239db53236d305cc7305149a0161fea075db6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Tue, 13 Aug 2024 09:39:41 +0200 Subject: [PATCH] splunk_logger: move environment hook to splunk_logger pt1 this has to be in two steps: - duplicate to splunk_logger first - change and use the reference from within go.mod and the main code --- pkg/splunk_logger/environment_hook.go | 26 +++++++++++++++++++ pkg/splunk_logger/environment_hook_test.go | 30 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 pkg/splunk_logger/environment_hook.go create mode 100644 pkg/splunk_logger/environment_hook_test.go diff --git a/pkg/splunk_logger/environment_hook.go b/pkg/splunk_logger/environment_hook.go new file mode 100644 index 0000000000..152b97197c --- /dev/null +++ b/pkg/splunk_logger/environment_hook.go @@ -0,0 +1,26 @@ +package logger + +import ( + "github.com/sirupsen/logrus" +) + +type EnvironmentHook struct { + Channel string +} + +func (h *EnvironmentHook) Levels() []logrus.Level { + return []logrus.Level{ + logrus.DebugLevel, + logrus.InfoLevel, + logrus.WarnLevel, + logrus.ErrorLevel, + logrus.FatalLevel, + logrus.PanicLevel, + } +} + +func (h *EnvironmentHook) Fire(e *logrus.Entry) error { + e.Data["channel"] = h.Channel + + return nil +} diff --git a/pkg/splunk_logger/environment_hook_test.go b/pkg/splunk_logger/environment_hook_test.go new file mode 100644 index 0000000000..1b7965e3f9 --- /dev/null +++ b/pkg/splunk_logger/environment_hook_test.go @@ -0,0 +1,30 @@ +package logger + +import ( + "bytes" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" +) + +func makeLogrus(buf *bytes.Buffer) *logrus.Logger { + return &logrus.Logger{ + Out: buf, + Formatter: &logrus.TextFormatter{ + DisableTimestamp: true, + DisableColors: true, + }, + Hooks: make(logrus.LevelHooks), + Level: logrus.DebugLevel, + } + +} + +func TestInfoWithEnvironment(t *testing.T) { + buf := &bytes.Buffer{} + l := makeLogrus(buf) + l.AddHook(&EnvironmentHook{Channel: "test_framework"}) + l.Info("test message") + require.Equal(t, "level=info msg=\"test message\" channel=test_framework\n", buf.String()) +}