Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go:
- 1.7.x
- 1.8
- 1.10.x
- 1.12.x
- master
install:
- go get github.com/sirupsen/logrus
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# discordrus | a [Discord](https://discordapp.com/) hook for [Logrus](https://github.com/Sirupsen/logrus) <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":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

Expand All @@ -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
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions discordrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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?")
Expand Down
4 changes: 4 additions & 0 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down
36 changes: 30 additions & 6 deletions levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestAllLevels(t *testing.T) {
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}

if !reflect.DeepEqual(AllLevels, logrus.AllLevels) {
Expand All @@ -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,
Expand All @@ -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")
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down