Skip to content

Commit

Permalink
splunk_logger: move environment hook to splunk_logger pt1
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schuellerf committed Aug 14, 2024
1 parent 812b331 commit 0239db5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/splunk_logger/environment_hook.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions pkg/splunk_logger/environment_hook_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 0239db5

Please sign in to comment.