-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logrus: add deployment channel as field to the logs
- Loading branch information
1 parent
2da3a73
commit 9006836
Showing
5 changed files
with
80 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package common | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package common | ||
|
||
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters