From e615b7d67124c548a3c7b422348821204ce32775 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 12 Jul 2018 14:34:37 -0400 Subject: Log all output of logrus to syslog as well as stdout/stderr Signed-off-by: Daniel J Walsh Closes: #1084 Approved by: baude --- cmd/podman/main.go | 12 +++++ completions/bash/podman | 1 + docs/podman.1.md | 4 ++ .../sirupsen/logrus/hooks/syslog/README.md | 39 ++++++++++++++++ .../sirupsen/logrus/hooks/syslog/syslog.go | 54 ++++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 vendor/github.com/sirupsen/logrus/hooks/syslog/README.md create mode 100644 vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go diff --git a/cmd/podman/main.go b/cmd/podman/main.go index fefd8fdad..f533a8b13 100644 --- a/cmd/podman/main.go +++ b/cmd/podman/main.go @@ -14,7 +14,9 @@ import ( "github.com/projectatomic/libpod/pkg/rootless" "github.com/projectatomic/libpod/version" "github.com/sirupsen/logrus" + lsyslog "github.com/sirupsen/logrus/hooks/syslog" "github.com/urfave/cli" + "log/syslog" ) // This is populated by the Makefile from the VERSION file @@ -94,6 +96,12 @@ func main() { } app.Before = func(c *cli.Context) error { + if c.GlobalBool("syslog") { + hook, err := lsyslog.NewSyslogHook("", "", syslog.LOG_INFO, "") + if err == nil { + logrus.AddHook(hook) + } + } logLevel := c.GlobalString("log-level") if logLevel != "" { level, err := logrus.ParseLevel(logLevel) @@ -187,6 +195,10 @@ func main() { Name: "storage-opt", Usage: "used to pass an option to the storage driver", }, + cli.BoolFlag{ + Name: "syslog", + Usage: "output logging information to syslog as well as the console", + }, } if _, err := os.Stat("/etc/containers/registries.conf"); err != nil { if os.IsNotExist(err) { diff --git a/completions/bash/podman b/completions/bash/podman index f80e0d5eb..c07987a86 100644 --- a/completions/bash/podman +++ b/completions/bash/podman @@ -2026,6 +2026,7 @@ _podman_podman() { local boolean_options=" --help -h --version -v + --syslog " commands=" attach diff --git a/docs/podman.1.md b/docs/podman.1.md index bb84037a0..c946ccb02 100644 --- a/docs/podman.1.md +++ b/docs/podman.1.md @@ -59,6 +59,10 @@ Select which storage driver is used to manage storage of images and containers ( Used to pass an option to the storage driver +**--syslog** + +output logging information to syslog as well as the console + **--version, -v** Print the version diff --git a/vendor/github.com/sirupsen/logrus/hooks/syslog/README.md b/vendor/github.com/sirupsen/logrus/hooks/syslog/README.md new file mode 100644 index 000000000..92b391c17 --- /dev/null +++ b/vendor/github.com/sirupsen/logrus/hooks/syslog/README.md @@ -0,0 +1,39 @@ +# Syslog Hooks for Logrus :walrus: + +## Usage + +```go +import ( + "log/syslog" + "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" +) + +func main() { + log := logrus.New() + hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") + + if err == nil { + log.Hooks.Add(hook) + } +} +``` + +If you want to connect to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). Just assign empty string to the first two parameters of `NewSyslogHook`. It should look like the following. + +```go +import ( + "log/syslog" + "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" +) + +func main() { + log := logrus.New() + hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "") + + if err == nil { + log.Hooks.Add(hook) + } +} +``` diff --git a/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go b/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go new file mode 100644 index 000000000..204f0016d --- /dev/null +++ b/vendor/github.com/sirupsen/logrus/hooks/syslog/syslog.go @@ -0,0 +1,54 @@ +// +build !windows,!nacl,!plan9 + +package logrus_syslog + +import ( + "fmt" + "github.com/sirupsen/logrus" + "log/syslog" + "os" +) + +// 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 { + line, err := entry.String() + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to read entry, %v", err) + return err + } + + switch entry.Level { + case logrus.PanicLevel: + return hook.Writer.Crit(line) + case logrus.FatalLevel: + return hook.Writer.Crit(line) + case logrus.ErrorLevel: + return hook.Writer.Err(line) + case logrus.WarnLevel: + return hook.Writer.Warning(line) + case logrus.InfoLevel: + return hook.Writer.Info(line) + case logrus.DebugLevel: + return hook.Writer.Debug(line) + default: + return nil + } +} + +func (hook *SyslogHook) Levels() []logrus.Level { + return logrus.AllLevels +} -- cgit v1.2.3-54-g00ecf