Skip to content

Commit 6cd87c6

Browse files
authored
Merge pull request #5 from lelvisl/4
Add sentry hook support
2 parents 85ecae3 + 0f252c2 commit 6cd87c6

File tree

6 files changed

+125
-11
lines changed

6 files changed

+125
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ module github.com/lelvisl/logger
22

33
require (
44
github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce
5+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 // indirect
6+
github.com/evalphobia/logrus_sentry v0.8.2
7+
github.com/getsentry/raven-go v0.2.0
58
github.com/onrik/logrus v0.2.1
9+
github.com/pkg/errors v0.8.1 // indirect
610
github.com/sirupsen/logrus v1.3.0
711
github.com/stretchr/testify v1.3.0 // indirect
812
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc
913
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb
14+
gopkg.in/yaml.v2 v2.2.2
1015
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce h1:GH0okCzXIZ3kJQr+D7X1/c4jEK51m1eW7nc63cRE6Yo=
22
github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
3+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4=
4+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ=
8+
github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
9+
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
10+
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
511
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
612
github.com/onrik/logrus v0.2.1 h1:xEYR+opLvr+hNixPPAimuQppFYHaZ0XLO9hZ2G8WPLI=
713
github.com/onrik/logrus v0.2.1/go.mod h1:qfe9NeZVAJfIxviw3cYkZo3kvBtLoPRJriAO8zl7qTk=
14+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
15+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
816
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
917
github.com/sirupsen/logrus v1.0.5 h1:8c8b5uO0zS4X6RPl/sd1ENwSkIc0/H2PaHxE3udaE8I=
1018
github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
@@ -24,3 +32,6 @@ golang.org/x/sys v0.0.0-20180326154331-13d03a9a82fb/go.mod h1:STP8DvDyc/dI5b8T5h
2432
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2533
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb h1:1w588/yEchbPNpa9sEvOcMZYbWHedwJjg4VOAdDHWHk=
2634
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
35+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
37+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

logger.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@ package logger
22

33
import (
44
"context"
5+
"os"
6+
57
"github.com/onrik/logrus/filename"
68
log "github.com/sirupsen/logrus"
7-
"os"
89
)
910

1011
//LogConfig is a configuration for logger
1112
type LogConfig struct {
12-
Title string `yaml:"title" json:"title" toml:"title"`
13-
Type string `yaml:"type" json:"type" toml:"type"`
14-
NetworkType string `yaml:"network type" json:"network_type" toml:"network_type"`
15-
Host string `yaml:"host" json:"host" toml:"host"`
16-
Severity string `yaml:"severity" json:"severity" toml:"severity"`
17-
Facility string `yaml:"facility" json:"facility" toml:"facility"`
18-
Port string `yaml:"port" json:"port" toml:"port"`
19-
FilePath string `yaml:"file path" json:"file_path" toml:"file_path"`
20-
FileName string `yaml:"file name" json:"file_name" toml:"file_name"`
21-
DebugMode bool `yaml:"debug mode" json:"debug_mode" toml:"debug_mode"`
13+
Title string `yaml:"title" json:"title" toml:"title"`
14+
Type string `yaml:"type" json:"type" toml:"type"`
15+
NetworkType string `yaml:"network type" json:"network_type" toml:"network_type"`
16+
Host string `yaml:"host" json:"host" toml:"host"`
17+
Severity string `yaml:"severity" json:"severity" toml:"severity"`
18+
Facility string `yaml:"facility" json:"facility" toml:"facility"`
19+
Port string `yaml:"port" json:"port" toml:"port"`
20+
FilePath string `yaml:"file path" json:"file_path" toml:"file_path"`
21+
FileName string `yaml:"file name" json:"file_name" toml:"file_name"`
22+
DebugMode bool `yaml:"debug mode" json:"debug_mode" toml:"debug_mode"`
23+
Sentry SentryConfig `yaml: "sentry" json:"sentry"`
24+
}
25+
26+
type SentryConfig struct {
27+
Tags map[string]string `yaml:"tags" json:"tags"`
28+
DSN string `yaml:"dsn" json:"dns"`
2229
}
2330

2431
type ctxlog struct{}
@@ -66,6 +73,12 @@ func initLogger(config LogConfig) *log.Logger {
6673
filenameHook := filename.NewHook()
6774
filenameHook.Field = "source" // Customize source field name
6875
logger.AddHook(filenameHook)
76+
if config.Sentry.DSN != "" {
77+
sh, err := sentryHook(&config)
78+
if err == nil {
79+
logger.AddHook(sh)
80+
}
81+
}
6982
switch config.Type {
7083
case "syslog":
7184
logger = initSyslogger(config)
@@ -79,6 +92,7 @@ func initLogger(config LogConfig) *log.Logger {
7992
return logger
8093
default:
8194
}
95+
8296
if config.DebugMode {
8397
logger.Out = os.Stdout
8498
}

logger_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package logger
2+
3+
import (
4+
"testing"
5+
)
6+
7+
//
8+
func TestCreateLogger(t *testing.T) {
9+
type testCase struct {
10+
Message string
11+
LogLevel string
12+
}
13+
14+
testCases := []testCase{
15+
{
16+
"Info text",
17+
"LOG_INFO",
18+
},
19+
{
20+
"Warn text",
21+
"LOG_WARNING",
22+
},
23+
{
24+
"Debug text",
25+
"LOG_DEBUG",
26+
},
27+
}
28+
29+
for _, tc := range testCases {
30+
c := LogConfig{
31+
Type: "stdout",
32+
Severity: tc.LogLevel,
33+
Sentry: SentryConfig{
34+
Tags: map[string]string{
35+
"site": "dev",
36+
},
37+
// TODO insert DNS for raven
38+
DSN: "",
39+
},
40+
}
41+
logger := CreateLogger(c)
42+
43+
logger.Infoln(tc.LogLevel, "Info text")
44+
logger.Warningln(tc.LogLevel, " Warn text")
45+
logger.Debugln(tc.LogLevel, "Debug text")
46+
}
47+
}

sentry.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package logger
2+
3+
import (
4+
"github.com/evalphobia/logrus_sentry"
5+
"github.com/getsentry/raven-go"
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
func logLevels(config *LogConfig) []log.Level {
10+
// TODO must
11+
ll := logLevel[config.Severity]
12+
levels := make([]log.Level, 0, ll+1)
13+
for i := log.PanicLevel; i <= ll; i++ {
14+
levels = append(levels, i)
15+
}
16+
return levels
17+
}
18+
19+
func sentryHook(config *LogConfig) (*logrus_sentry.SentryHook, error) {
20+
var (
21+
hook *logrus_sentry.SentryHook
22+
err error
23+
)
24+
levels := logLevels(config)
25+
client, err := raven.New(config.Sentry.DSN)
26+
if err != nil {
27+
return nil, err
28+
}
29+
if len(config.Sentry.Tags) != 0 {
30+
client.Tags = config.Sentry.Tags
31+
}
32+
33+
hook, err = logrus_sentry.NewWithClientSentryHook(client, levels)
34+
35+
return hook, err
36+
}

0 commit comments

Comments
 (0)