diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/go.mod b/go.mod index 3d8e2c3..30c6253 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,14 @@ module github.com/lelvisl/logger require ( github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce + github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 // indirect + github.com/evalphobia/logrus_sentry v0.8.2 + github.com/getsentry/raven-go v0.2.0 github.com/onrik/logrus v0.2.1 + github.com/pkg/errors v0.8.1 // indirect github.com/sirupsen/logrus v1.3.0 github.com/stretchr/testify v1.3.0 // indirect golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb + gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index b299fde..30a1fe6 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,18 @@ github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce h1:GH0okCzXIZ3kJQr+D7X1/c4jEK51m1eW7nc63cRE6Yo= github.com/bshuster-repo/logrus-logstash-hook v0.0.0-20180322175230-ea59b04518367f2aafe8cf6d46a7d60605d075ce/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk= +github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4= +github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/evalphobia/logrus_sentry v0.8.2 h1:dotxHq+YLZsT1Bb45bB5UQbfCh3gM/nFFetyN46VoDQ= +github.com/evalphobia/logrus_sentry v0.8.2/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc= +github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs= +github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/onrik/logrus v0.2.1 h1:xEYR+opLvr+hNixPPAimuQppFYHaZ0XLO9hZ2G8WPLI= github.com/onrik/logrus v0.2.1/go.mod h1:qfe9NeZVAJfIxviw3cYkZo3kvBtLoPRJriAO8zl7qTk= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.0.5 h1:8c8b5uO0zS4X6RPl/sd1ENwSkIc0/H2PaHxE3udaE8I= 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 golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb h1:1w588/yEchbPNpa9sEvOcMZYbWHedwJjg4VOAdDHWHk= golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/logger.go b/logger.go index 11580f6..c8adf22 100644 --- a/logger.go +++ b/logger.go @@ -2,23 +2,30 @@ package logger import ( "context" + "os" + "github.com/onrik/logrus/filename" log "github.com/sirupsen/logrus" - "os" ) //LogConfig is a configuration for logger type LogConfig struct { - Title string `yaml:"title" json:"title" toml:"title"` - Type string `yaml:"type" json:"type" toml:"type"` - NetworkType string `yaml:"network type" json:"network_type" toml:"network_type"` - Host string `yaml:"host" json:"host" toml:"host"` - Severity string `yaml:"severity" json:"severity" toml:"severity"` - Facility string `yaml:"facility" json:"facility" toml:"facility"` - Port string `yaml:"port" json:"port" toml:"port"` - FilePath string `yaml:"file path" json:"file_path" toml:"file_path"` - FileName string `yaml:"file name" json:"file_name" toml:"file_name"` - DebugMode bool `yaml:"debug mode" json:"debug_mode" toml:"debug_mode"` + Title string `yaml:"title" json:"title" toml:"title"` + Type string `yaml:"type" json:"type" toml:"type"` + NetworkType string `yaml:"network type" json:"network_type" toml:"network_type"` + Host string `yaml:"host" json:"host" toml:"host"` + Severity string `yaml:"severity" json:"severity" toml:"severity"` + Facility string `yaml:"facility" json:"facility" toml:"facility"` + Port string `yaml:"port" json:"port" toml:"port"` + FilePath string `yaml:"file path" json:"file_path" toml:"file_path"` + FileName string `yaml:"file name" json:"file_name" toml:"file_name"` + DebugMode bool `yaml:"debug mode" json:"debug_mode" toml:"debug_mode"` + Sentry SentryConfig `yaml: "sentry" json:"sentry"` +} + +type SentryConfig struct { + Tags map[string]string `yaml:"tags" json:"tags"` + DSN string `yaml:"dsn" json:"dns"` } type ctxlog struct{} @@ -66,6 +73,12 @@ func initLogger(config LogConfig) *log.Logger { filenameHook := filename.NewHook() filenameHook.Field = "source" // Customize source field name logger.AddHook(filenameHook) + if config.Sentry.DSN != "" { + sh, err := sentryHook(&config) + if err == nil { + logger.AddHook(sh) + } + } switch config.Type { case "syslog": logger = initSyslogger(config) @@ -79,6 +92,7 @@ func initLogger(config LogConfig) *log.Logger { return logger default: } + if config.DebugMode { logger.Out = os.Stdout } diff --git a/logger_test.go b/logger_test.go new file mode 100644 index 0000000..8c79667 --- /dev/null +++ b/logger_test.go @@ -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") + } +} diff --git a/sentry.go b/sentry.go new file mode 100644 index 0000000..d05338a --- /dev/null +++ b/sentry.go @@ -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 +}