Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a flag for splitting log output (--log-splitting) #775

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"bytes"
"net/http"
"os"
"os/signal"
Expand All @@ -36,6 +37,17 @@ import (
"github.com/kubernetes-incubator/external-dns/source"
)

type logOutputSplitter struct{}

// Splits log output, error and fatal to stderr and the rest to stdout
func (splitter *logOutputSplitter) Write(p []byte) (n int, err error) {
if bytes.Contains(p, []byte("level=debug")) || bytes.Contains(p, []byte("level=info")) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks pretty hacky I must say

Copy link
Author

@freddd freddd Nov 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. It is hacky. However I'm not really sure how to solve it in a nicer way as the only thing we can act on is the log line. Looking at the references it seems like they are not intending to fix it in logrus (especially not now since it is, according to the README, looking for a maintainer).

Any suggestions on how I could solve it in a nicer way? It's really annoying that all logs from external-dns shows up as errors, even if it's not.

bytes.Contains(p, []byte("level=trace")) || bytes.Contains(p, []byte("level=warn")) {
return os.Stdout.Write(p)
}
return os.Stderr.Write(p)
}

func main() {
cfg := externaldns.NewConfig()
if err := cfg.ParseFlags(os.Args[1:]); err != nil {
Expand All @@ -50,6 +62,12 @@ func main() {
if cfg.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
}

if cfg.LogSplitting {
log.SetOutput(&logOutputSplitter{})
log.Info("Configured splitting of logs, error & fatal to stderr and the rest to stdout")
}

if cfg.DryRun {
log.Info("running in dry-run mode. No changes to DNS records will be made.")
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Config struct {
RFC2136TSIGSecret string
RFC2136TSIGSecretAlg string
RFC2136TAXFR bool
LogSplitting bool
}

var defaultConfig = &Config{
Expand Down Expand Up @@ -173,6 +174,7 @@ var defaultConfig = &Config{
RFC2136TSIGSecret: "",
RFC2136TSIGSecretAlg: "",
RFC2136TAXFR: true,
LogSplitting: false,
}

// NewConfig returns new Config object
Expand Down Expand Up @@ -302,6 +304,7 @@ func (cfg *Config) ParseFlags(args []string) error {
app.Flag("log-format", "The format in which log messages are printed (default: text, options: text, json)").Default(defaultConfig.LogFormat).EnumVar(&cfg.LogFormat, "text", "json")
app.Flag("metrics-address", "Specify where to serve the metrics and health check endpoint (default: :7979)").Default(defaultConfig.MetricsAddress).StringVar(&cfg.MetricsAddress)
app.Flag("log-level", "Set the level of logging. (default: info, options: panic, debug, info, warning, error, fatal").Default(defaultConfig.LogLevel).EnumVar(&cfg.LogLevel, allLogLevelsAsStrings()...)
app.Flag("log-splitting", "Splits log output, below 'error' to stdout and above & including 'error' to stderr (default: false)").BoolVar(&cfg.LogSplitting)

_, err := app.Parse(args)
if err != nil {
Expand Down