Skip to content

Commit

Permalink
[logrus] add output hook to optionally direct trace, info, debug, and…
Browse files Browse the repository at this point in the history
… warn to stdout

Addresses #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.
  • Loading branch information
tredman committed Nov 8, 2018
1 parent 7ecbe54 commit 9bfab3c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
Watchers []*WatcherConfig
Verbosity string
LegacyLogPaths bool `yaml:"legacyLogPaths"`
SplitLogging bool `yaml:"splitLogging"`
}

type WatcherConfig struct {
Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"os"
"os/signal"
Expand All @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 9bfab3c

Please sign in to comment.