Skip to content

Commit

Permalink
Make sentry only hook
Browse files Browse the repository at this point in the history
  • Loading branch information
devopsbot-mts committed Mar 1, 2019
1 parent 32854b1 commit 0f252c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
31 changes: 15 additions & 16 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ 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"`
Sentry SentryConfig `yaml: "sentry" json:"sentry"`
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 {
Expand Down Expand Up @@ -73,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)
Expand All @@ -84,16 +90,9 @@ func initLogger(config LogConfig) *log.Logger {
case "stderr":
logger.Out = os.Stderr
return logger
case "sentry":
logger = initSentrylogger(config)
default:
}
if config.Sentry.DSN != "" {
sh, err := sentryHook(&config)
if err == nil {
logger.AddHook(sh)
}
}

if config.DebugMode {
logger.Out = os.Stdout
}
Expand Down
1 change: 1 addition & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestCreateLogger(t *testing.T) {
},
}
logger := CreateLogger(c)

logger.Infoln(tc.LogLevel, "Info text")
logger.Warningln(tc.LogLevel, " Warn text")
logger.Debugln(tc.LogLevel, "Debug text")
Expand Down
27 changes: 7 additions & 20 deletions sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,21 @@ func logLevels(config *LogConfig) []log.Level {
return levels
}

func initSentrylogger(config LogConfig) *log.Logger {
var (
err error
)
logger := log.New()
_, err = raven.New(config.Sentry.DSN)
if err != nil {
log.Fatal(err)
}

hook, err := sentryHook(&config)
if err == nil {
logger.Hooks.Add(hook)
}

return logger
}

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 {
hook, err = logrus_sentry.NewWithTagsSentryHook(config.Sentry.DSN, config.Sentry.Tags, logLevels(config))
client.Tags = config.Sentry.Tags
}

hook, err = logrus_sentry.NewSentryHook(config.Sentry.DSN, logLevels(config))
hook, err = logrus_sentry.NewWithClientSentryHook(client, levels)

return hook, err
}

0 comments on commit 0f252c2

Please sign in to comment.