From b799ea63523903a86afff991b294311635917431 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Mon, 20 May 2019 21:44:39 +0100 Subject: [PATCH] Add trace logging level; fix maximum username overflow test --- .travis.yml | 1 + README.md | 9 ++++----- discordrus_test.go | 8 +++++--- levels.go | 4 ++++ levels_test.go | 36 ++++++++++++++++++++++++++++++------ 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1edf8e4..4d4b89d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ go: - 1.7.x - 1.8 - 1.10.x + - 1.12.x - master install: - go get github.com/sirupsen/logrus diff --git a/README.md b/README.md index 8f04f9a..7ba7a89 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # discordrus | a [Discord](https://discordapp.com/) hook for [Logrus](https://github.com/Sirupsen/logrus) :walrus: [![Travis CI](https://api.travis-ci.org/kz/discordrus.svg?branch=master)](https://travis-ci.org/kz/discordrus) [![GoDoc](https://godoc.org/github.com/puddingfactory/logentrus?status.svg)](https://godoc.org/github.com/kz/discordrus) -**Current version:** v1.1.1 - -![Screenshot of discordrus in action](http://i.imgur.com/zvDNDjV.png) +![Screenshot of discordrus in action](https://i.imgur.com/q8Tcmjn.png?1) ## Install @@ -29,13 +27,13 @@ import ( func init() { logrus.SetFormatter(&logrus.TextFormatter{}) logrus.SetOutput(os.Stderr) - logrus.SetLevel(logrus.DebugLevel) + logrus.SetLevel(logrus.TraceLevel) logrus.AddHook(discordrus.NewHook( // Use environment variable for security reasons os.Getenv("DISCORDRUS_WEBHOOK_URL"), // Set minimum level to DebugLevel to receive all log entries - logrus.DebugLevel, + logrus.TraceLevel, &discordrus.Opts{ Username: "Test Username", Author: "", // Setting this to a non-empty string adds the author text to the message header @@ -44,6 +42,7 @@ func init() { TimestampLocale: nil, // The timestamp uses this locale; if it is unset, it will use time.Local EnableCustomColors: true, // If set to true, the below CustomLevelColors will apply CustomLevelColors: &discordrus.LevelColors{ + Trace: 3092790, Debug: 10170623, Info: 3581519, Warn: 14327864, diff --git a/discordrus_test.go b/discordrus_test.go index 38c63a6..2ddb02c 100644 --- a/discordrus_test.go +++ b/discordrus_test.go @@ -14,13 +14,13 @@ import ( func init() { logrus.SetFormatter(&logrus.TextFormatter{}) logrus.SetOutput(os.Stderr) - logrus.SetLevel(logrus.DebugLevel) + logrus.SetLevel(logrus.TraceLevel) logrus.AddHook(NewHook( // Use environment variable for security reasons os.Getenv("DISCORDRUS_WEBHOOK_URL"), // Set minimum level to DebugLevel to receive all log entries - logrus.DebugLevel, + logrus.TraceLevel, &Opts{ Username: "Test Username", Author: "", // Setting this to a non-empty string adds the author text to the message header @@ -29,6 +29,7 @@ func init() { TimestampLocale: nil, // The timestamp uses this locale; if it is unset, it will use time.Local EnableCustomColors: true, // If set to true, the below CustomLevelColors will apply CustomLevelColors: &LevelColors{ + Trace: 3092790, Debug: 10170623, Info: 3581519, Warn: 14327864, @@ -93,7 +94,7 @@ func TestMaxLengths(t *testing.T) { { name: "usernameMaxPlusOne", json: "{\"embeds\":[{\"author\":{\"name\":\"A\"},\"description\":\"A\",\"fields\":[{\"name\":\"A\",\"value\":\"A\"}],\"title\":\"A\"}],\"username\":\"" + strings.Repeat("A", maxUsernameChars+1) + "\"}", - expectedStatusCode: 400, + expectedStatusCode: 204, }, { name: "fieldNameMax", @@ -141,6 +142,7 @@ func TestMaxLengths(t *testing.T) { // TestHookIntegration is an integration test to ensure that log entries do send func TestHookIntegration(t *testing.T) { + logrus.WithFields(logrus.Fields{"String": "hi", "Integer": 2, "Boolean": false}).Trace("Check this out! Awesome, right?") logrus.WithFields(logrus.Fields{"String": "hi", "Integer": 2, "Boolean": false}).Debug("Check this out! Awesome, right?") logrus.WithFields(logrus.Fields{"String": "hi", "Integer": 2, "Boolean": false}).Info("Check this out! Awesome, right?") logrus.WithFields(logrus.Fields{"String": "hi", "Integer": 2, "Boolean": false}).Warn("Check this out! Awesome, right?") diff --git a/levels.go b/levels.go index 3163805..67248eb 100644 --- a/levels.go +++ b/levels.go @@ -6,6 +6,7 @@ import ( // LevelColors is a struct of the possible colors used in Discord color format (0x[RGB] converted to int) type LevelColors struct { + Trace int Debug int Info int Warn int @@ -16,6 +17,7 @@ type LevelColors struct { // DefaultLevelColors is a struct of the default colors used var DefaultLevelColors = LevelColors{ + Trace: 3092790, Debug: 10170623, Info: 3581519, Warn: 14327864, @@ -32,6 +34,8 @@ func LevelThreshold(l logrus.Level) []logrus.Level { // LevelColor returns the respective color for the logrus level func (lc LevelColors) LevelColor(l logrus.Level) int { switch l { + case logrus.TraceLevel: + return lc.Trace case logrus.DebugLevel: return lc.Debug case logrus.InfoLevel: diff --git a/levels_test.go b/levels_test.go index d281e0f..cb0a5f8 100644 --- a/levels_test.go +++ b/levels_test.go @@ -17,6 +17,7 @@ func TestAllLevels(t *testing.T) { logrus.WarnLevel, logrus.InfoLevel, logrus.DebugLevel, + logrus.TraceLevel, } if !reflect.DeepEqual(AllLevels, logrus.AllLevels) { @@ -26,6 +27,15 @@ func TestAllLevels(t *testing.T) { // TestLevelThreshold ensures that the slice returned contains the correct level func TestLevelThreshold(t *testing.T) { + var expectedTraceThreshold = []logrus.Level{ + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel, + logrus.WarnLevel, + logrus.InfoLevel, + logrus.DebugLevel, + logrus.TraceLevel, + } var expectedDebugThreshold = []logrus.Level{ logrus.PanicLevel, logrus.FatalLevel, @@ -44,6 +54,11 @@ func TestLevelThreshold(t *testing.T) { } // Test extreme boundaries + traceThreshold := LevelThreshold(logrus.TraceLevel) + if !reflect.DeepEqual(traceThreshold, expectedTraceThreshold) { + t.Error("Trace threshold does not match expected slice") + } + debugThreshold := LevelThreshold(logrus.DebugLevel) if !reflect.DeepEqual(debugThreshold, expectedDebugThreshold) { t.Error("Debug threshold does not match expected slice") @@ -65,18 +80,23 @@ func TestLevelThreshold(t *testing.T) { func TestLevelColor(t *testing.T) { // Set up custom LevelColors customLevelColors := LevelColors{ - Debug: 1, - Info: 2, - Warn: 3, - Error: 4, - Panic: 5, - Fatal: 6, + Trace: 1, + Debug: 2, + Info: 3, + Warn: 4, + Error: 5, + Panic: 6, + Fatal: 7, } defaultColors := []struct { expected int input logrus.Level }{ + { + input: logrus.TraceLevel, + expected: DefaultLevelColors.Trace, + }, { input: logrus.DebugLevel, expected: DefaultLevelColors.Debug, @@ -113,6 +133,10 @@ func TestLevelColor(t *testing.T) { expected int input logrus.Level }{ + { + expected: customLevelColors.Trace, + input: logrus.TraceLevel, + }, { expected: customLevelColors.Debug, input: logrus.DebugLevel,