-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from lelvisl/4
Add sentry hook support
- Loading branch information
Showing
6 changed files
with
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
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
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,47 @@ | ||
package logger | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// | ||
func TestCreateLogger(t *testing.T) { | ||
type testCase struct { | ||
Message string | ||
LogLevel string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
"Info text", | ||
"LOG_INFO", | ||
}, | ||
{ | ||
"Warn text", | ||
"LOG_WARNING", | ||
}, | ||
{ | ||
"Debug text", | ||
"LOG_DEBUG", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
c := LogConfig{ | ||
Type: "stdout", | ||
Severity: tc.LogLevel, | ||
Sentry: SentryConfig{ | ||
Tags: map[string]string{ | ||
"site": "dev", | ||
}, | ||
// TODO insert DNS for raven | ||
DSN: "", | ||
}, | ||
} | ||
logger := CreateLogger(c) | ||
|
||
logger.Infoln(tc.LogLevel, "Info text") | ||
logger.Warningln(tc.LogLevel, " Warn text") | ||
logger.Debugln(tc.LogLevel, "Debug text") | ||
} | ||
} |
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,36 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/evalphobia/logrus_sentry" | ||
"github.com/getsentry/raven-go" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func logLevels(config *LogConfig) []log.Level { | ||
// TODO must | ||
ll := logLevel[config.Severity] | ||
levels := make([]log.Level, 0, ll+1) | ||
for i := log.PanicLevel; i <= ll; i++ { | ||
levels = append(levels, i) | ||
} | ||
return levels | ||
} | ||
|
||
func sentryHook(config *LogConfig) (*logrus_sentry.SentryHook, error) { | ||
var ( | ||
hook *logrus_sentry.SentryHook | ||
err error | ||
) | ||
levels := logLevels(config) | ||
client, err := raven.New(config.Sentry.DSN) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(config.Sentry.Tags) != 0 { | ||
client.Tags = config.Sentry.Tags | ||
} | ||
|
||
hook, err = logrus_sentry.NewWithClientSentryHook(client, levels) | ||
|
||
return hook, err | ||
} |