From 9bfab3c91250df72ae27e264d85b2f7eed48d9d5 Mon Sep 17 00:00:00 2001 From: Travis Redman Date: Thu, 8 Nov 2018 10:53:43 -0800 Subject: [PATCH] [logrus] add output hook to optionally direct trace, info, debug, and warn to stdout Addresses https://github.com/honeycombio/honeycomb-kubernetes-agent/issues/26 by providing a new config option, `splitLogging`, to enable certain log severities to go to stdout rather than stderr. I chose to make this optional as I do not want to introduce new behavior into the agent for all users, which may rely on everything going to stderr. --- config/config.go | 1 + main.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/config/config.go b/config/config.go index 353cc63b..f0e25abe 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ type Config struct { Watchers []*WatcherConfig Verbosity string LegacyLogPaths bool `yaml:"legacyLogPaths"` + SplitLogging bool `yaml:"splitLogging"` } type WatcherConfig struct { diff --git a/main.go b/main.go index d329bf22..dd6a2b7e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "os" "os/signal" @@ -22,6 +23,18 @@ import ( "k8s.io/client-go/rest" ) +type OutputSplitter struct{} + +// Some systems that process logs expect warn, info, debug, and trace to be on stdout +// and all error output to go to stderr. +func (splitter *OutputSplitter) Write(p []byte) (n int, err error) { + if bytes.Contains(p, []byte("level=debug")) || bytes.Contains(p, []byte("level=info")) || + bytes.Contains(p, []byte("level=trace")) || bytes.Contains(p, []byte("level=warn")) { + return os.Stdout.Write(p) + } + return os.Stderr.Write(p) +} + type CmdLineOptions struct { ConfigPath string `long:"config" description:"Path to configuration file" default:"/etc/honeycomb/config.yaml"` Validate bool `long:"validate" description:"Validate configuration and exit"` @@ -66,6 +79,11 @@ func main() { os.Exit(1) } + if config.SplitLogging { + logrus.SetOutput(&OutputSplitter{}) + logrus.Info("Configured split logging. trace, debug, info, and warn levels will now go to stdout") + } + if config.Verbosity == "debug" { logrus.SetLevel(logrus.DebugLevel) }