Skip to content

Commit

Permalink
Do not override syslog target, fixes #2550
Browse files Browse the repository at this point in the history
Whenever teleport was started with -d flag,
output target was always overwritten to stderr.

This commit makes sure that target supplied
in the configuration is not overwritten
even in case when -d flag is set.
  • Loading branch information
klizhentas committed Feb 9, 2019
1 parent cf7766b commit 384de17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ const (

// HumanDateFormatMilli is a human readable date formatting with milliseconds
HumanDateFormatMilli = "Jan _2 15:04:05.000 UTC"

// DebugLevel is a debug logging level name
DebugLevel = "debug"
)

// Component generates "component:subcomponent1:subcomponent2" strings used
Expand Down
18 changes: 13 additions & 5 deletions lib/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ func ApplyFileConfig(fc *FileConfig, cfg *service.Config) error {
case "stdout", "out", "1":
log.SetOutput(os.Stdout)
case teleport.Syslog:
utils.SwitchLoggingtoSyslog()
err := utils.SwitchLoggingtoSyslog()
if err != nil {
// this error will go to stderr
log.Errorf("Failed to switch logging to syslog: %v.", err)
}
default:
// assume it's a file path:
logFile, err := os.Create(fc.Logger.Output)
Expand All @@ -219,14 +223,13 @@ func ApplyFileConfig(fc *FileConfig, cfg *service.Config) error {
log.SetLevel(log.InfoLevel)
case "err", "error":
log.SetLevel(log.ErrorLevel)
case "debug":
case teleport.DebugLevel:
log.SetLevel(log.DebugLevel)
case "warn", "warning":
log.SetLevel(log.WarnLevel)
default:
return trace.BadParameter("unsupported logger severity: '%v'", fc.Logger.Severity)
}

// apply cache policy for node and proxy
cachePolicy, err := fc.CachePolicy.Parse()
if err != nil {
Expand Down Expand Up @@ -807,6 +810,12 @@ func Configure(clf *CommandLineFlags, cfg *service.Config) error {
return trace.Wrap(err)
}
}

// apply command line --debug flag to override logger severity
if clf.Debug {
fileConf.Logger.Severity = teleport.DebugLevel
}

if err = ApplyFileConfig(fileConf, cfg); err != nil {
return trace.Wrap(err)
}
Expand All @@ -825,10 +834,9 @@ func Configure(clf *CommandLineFlags, cfg *service.Config) error {
cfg.Proxy.DisableTLS = clf.DisableTLS
}

// apply --debug flag:
// apply --debug flag to config:
if clf.Debug {
cfg.Console = ioutil.Discard
utils.InitLogger(utils.LoggingForDaemon, log.DebugLevel)
cfg.Debug = clf.Debug
}

Expand Down
14 changes: 8 additions & 6 deletions lib/utils/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ import (
"log/syslog"
"os"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
logrusSyslog "github.com/sirupsen/logrus/hooks/syslog"
)

// SwitchLoggingtoSyslog tells the logger to send the output to syslog. This
// code is behind a build flag because Windows does not support syslog.
func SwitchLoggingtoSyslog() {
func SwitchLoggingtoSyslog() error {
log.StandardLogger().SetHooks(make(log.LevelHooks))
hook, err := logrusSyslog.NewSyslogHook("", "", syslog.LOG_WARNING, "")
if err != nil {
// syslog not available
// syslog is not available
log.SetOutput(os.Stderr)
} else {
// ... and disable stderr:
log.AddHook(hook)
log.SetOutput(ioutil.Discard)
return trace.Wrap(err)
}
log.AddHook(hook)
// ... and disable stderr:
log.SetOutput(ioutil.Discard)
return nil
}

0 comments on commit 384de17

Please sign in to comment.