From 5c909080d7bbd2abf674aa245a61febfc4a28430 Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Fri, 18 Jul 2014 00:28:40 -0700 Subject: [PATCH 1/7] SyslogHook --- hooks/syslog/syslog.go | 61 +++++++++++++++++++++++++++++++++++++ hooks/syslog/syslog_test.go | 26 ++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 hooks/syslog/syslog.go create mode 100644 hooks/syslog/syslog_test.go diff --git a/hooks/syslog/syslog.go b/hooks/syslog/syslog.go new file mode 100644 index 0000000..09bb1f4 --- /dev/null +++ b/hooks/syslog/syslog.go @@ -0,0 +1,61 @@ +package logrus_syslog + +import ( + "os" + "fmt" + "log/syslog" + "github.com/Sirupsen/logrus" +) + +// SyslogHook to send logs via syslog. +type SyslogHook struct{ + Writer *syslog.Writer + SyslogNetwork string + SyslogRaddr string +} + +// Creates a hook to be added to an instance of logger. This is called with +// `hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_DEBUG, "")` +// `if err == nil { log.Hooks.Add(hook) }` +func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) (*SyslogHook, error) { + w, err := syslog.Dial(network, raddr, priority, tag) + return &SyslogHook{w, network, raddr}, err +} + +func (hook *SyslogHook) Fire(entry *logrus.Entry) error { + reader, err := entry.Reader() + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err) + return err + } + + line := reader.String() + + switch entry.Data["level"] { + case "panic": + return hook.Writer.Crit(line) + case "fatal": + return hook.Writer.Crit(line) + case "error": + return hook.Writer.Err(line) + case "warn": + return hook.Writer.Warning(line) + case "info": + return hook.Writer.Info(line) + case "debug": + return hook.Writer.Debug(line) + default: + return nil + } +} + +func (hook *SyslogHook) Levels() []logrus.Level { + return []logrus.Level{ + logrus.Panic, + logrus.Fatal, + logrus.Error, + logrus.Warn, + logrus.Info, + logrus.Debug, + } +} diff --git a/hooks/syslog/syslog_test.go b/hooks/syslog/syslog_test.go new file mode 100644 index 0000000..87d90df --- /dev/null +++ b/hooks/syslog/syslog_test.go @@ -0,0 +1,26 @@ +package logrus_syslog + +import ( + "testing" + "log/syslog" + "github.com/Sirupsen/logrus" +) + +func TestLocalhostAddAndPrint(t *testing.T) { + log := logrus.New() + hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + + if err != nil { + t.Errorf("Unable to connect to local syslog.") + } + + log.Hooks.Add(hook) + + for _, level := range hook.Levels() { + if len(log.Hooks[level]) != 1 { + t.Errorf("SyslogHook was not added. The length of log.Hooks[%v]: %v", level, len(log.Hooks[level])) + } + } + + log.Info("Congratulations!") +} \ No newline at end of file From 58cc2b3fec9c4cce0e5797fc26db6d4d23842c82 Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Fri, 18 Jul 2014 15:45:49 -0700 Subject: [PATCH 2/7] Added README --- hooks/syslog/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 hooks/syslog/README.md diff --git a/hooks/syslog/README.md b/hooks/syslog/README.md new file mode 100644 index 0000000..74dc73f --- /dev/null +++ b/hooks/syslog/README.md @@ -0,0 +1,20 @@ +# Syslog Hooks for Logrus :walrus: + +## Usage + +```go +import ( + "log/syslog" + "github.com/Sirupsen/logrus" + "github.com/Sirupsen/logrus/hooks/syslog" +) + +func main() { + log := logrus.New() + hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + + if err == nil { + log.Hooks.Add(hook) + } +} +``` \ No newline at end of file From 24b6c463ab744d1029fc92ed0adaf49db1761083 Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Fri, 18 Jul 2014 15:47:34 -0700 Subject: [PATCH 3/7] Update frontpage README --- README.md | 3 +++ hooks/syslog/README.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41e23ef..fb98376 100644 --- a/README.md +++ b/README.md @@ -148,12 +148,15 @@ Logrus comes with built-in hooks. Add those, or your custom hook, in `init`: ```go import ( + "log/syslog" "github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus/hooks/airbrake" + "github.com/Sirupsen/logrus/hooks/syslog" ) func init() { log.Hooks.Add(new(logrus_airbrake.AirbrakeHook)) + log.Hooks.Add(logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")) } ``` diff --git a/hooks/syslog/README.md b/hooks/syslog/README.md index 74dc73f..cd706bc 100644 --- a/hooks/syslog/README.md +++ b/hooks/syslog/README.md @@ -11,7 +11,7 @@ import ( func main() { log := logrus.New() - hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") if err == nil { log.Hooks.Add(hook) From 90a435e26a6095350f3ae16a832b21813c90b11d Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Fri, 18 Jul 2014 15:48:56 -0700 Subject: [PATCH 4/7] Update more README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index fb98376..ef107e7 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,10 @@ func init() { Send errors to an exception tracking service compatible with the Airbrake API. Uses [`airbrake-go`](https://github.com/tobi/airbrake-go) behind the scenes. +* [`github.com/Sirupsen/logrus/hooks/syslog`](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go). + Send errors to remote syslog server. + Uses standard library `log/syslog` behind the scenes. + #### Level logging Logrus has six logging levels: Debug, Info, Warning, Error, Fatal and Panic. From ffc6dc63efc3ee48c94bb101f897553e6fbca4ff Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Sun, 27 Jul 2014 22:01:56 -0700 Subject: [PATCH 5/7] go fmt --- hooks/syslog/syslog.go | 12 ++++++------ hooks/syslog/syslog_test.go | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hooks/syslog/syslog.go b/hooks/syslog/syslog.go index 09bb1f4..137901e 100644 --- a/hooks/syslog/syslog.go +++ b/hooks/syslog/syslog.go @@ -1,17 +1,17 @@ package logrus_syslog import ( - "os" "fmt" - "log/syslog" "github.com/Sirupsen/logrus" + "log/syslog" + "os" ) // SyslogHook to send logs via syslog. -type SyslogHook struct{ - Writer *syslog.Writer - SyslogNetwork string - SyslogRaddr string +type SyslogHook struct { + Writer *syslog.Writer + SyslogNetwork string + SyslogRaddr string } // Creates a hook to be added to an instance of logger. This is called with diff --git a/hooks/syslog/syslog_test.go b/hooks/syslog/syslog_test.go index 87d90df..42762dc 100644 --- a/hooks/syslog/syslog_test.go +++ b/hooks/syslog/syslog_test.go @@ -1,14 +1,14 @@ package logrus_syslog import ( - "testing" - "log/syslog" "github.com/Sirupsen/logrus" + "log/syslog" + "testing" ) func TestLocalhostAddAndPrint(t *testing.T) { - log := logrus.New() - hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + log := logrus.New() + hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") if err != nil { t.Errorf("Unable to connect to local syslog.") @@ -23,4 +23,4 @@ func TestLocalhostAddAndPrint(t *testing.T) { } log.Info("Congratulations!") -} \ No newline at end of file +} From 491a120617542fc70893df6a1807364910f0d8c4 Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Sun, 27 Jul 2014 22:04:55 -0700 Subject: [PATCH 6/7] Call entry.String() directly. --- hooks/syslog/syslog.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hooks/syslog/syslog.go b/hooks/syslog/syslog.go index 137901e..d7b1343 100644 --- a/hooks/syslog/syslog.go +++ b/hooks/syslog/syslog.go @@ -23,14 +23,12 @@ func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) } func (hook *SyslogHook) Fire(entry *logrus.Entry) error { - reader, err := entry.Reader() + line, err := entry.String() if err != nil { fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err) return err } - line := reader.String() - switch entry.Data["level"] { case "panic": return hook.Writer.Crit(line) From 2a6183b10643fb31bd0a15314b4987517cc3eb07 Mon Sep 17 00:00:00 2001 From: Didip Kerabat Date: Sun, 27 Jul 2014 22:43:21 -0700 Subject: [PATCH 7/7] Updated to use new style of log level. --- hooks/syslog/syslog.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hooks/syslog/syslog.go b/hooks/syslog/syslog.go index d7b1343..2a18ce6 100644 --- a/hooks/syslog/syslog.go +++ b/hooks/syslog/syslog.go @@ -49,11 +49,11 @@ func (hook *SyslogHook) Fire(entry *logrus.Entry) error { func (hook *SyslogHook) Levels() []logrus.Level { return []logrus.Level{ - logrus.Panic, - logrus.Fatal, - logrus.Error, - logrus.Warn, - logrus.Info, - logrus.Debug, + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel, + logrus.WarnLevel, + logrus.InfoLevel, + logrus.DebugLevel, } }