Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fix #527: Do not log to stdout if log-path is set when starting daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
geauxvirtual committed Nov 23, 2015
1 parent 2483d23 commit e6e0668
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ func main() {
}

func action(ctx *cli.Context) {
// If logPath is set, we verify the logPath and set it so that all logging
// goes to the log file instead of stdout.
logPath := ctx.String("log-path")
if logPath != "" {
f, err := os.Stat(logPath)
if err != nil {
log.Fatal(err)
}
if !f.IsDir() {
log.Fatal("log path provided must be a directory")
}

file, err := os.OpenFile(fmt.Sprintf("%s/pulse.log", logPath), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
}

var l = map[int]string{
1: "debug",
2: "info",
Expand All @@ -192,7 +212,6 @@ func action(ctx *cli.Context) {
}

logLevel := ctx.Int("log-level")
logPath := ctx.String("log-path")
maxProcs := ctx.Int("max-procs")
disableAPI := ctx.Bool("disable-api")
apiPort := ctx.Int("api-port")
Expand Down Expand Up @@ -223,46 +242,6 @@ func action(ctx *cli.Context) {
// Validate log level and trust level settings for pulsed
validateLevelSettings(logLevel, pluginTrust)

if logPath != "" {
f, err := os.Stat(logPath)
if err != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "pulsed",
"error": err.Error(),
"logpath": logPath,
}).Fatal("bad log path (must be a dir)")
os.Exit(1)
}
if !f.IsDir() {
log.WithFields(
log.Fields{
"block": "main",
"_module": "pulsed",
"logpath": logPath,
}).Fatal("bad log path (this is not a directory)")
os.Exit(1)
}

file, err2 := os.OpenFile(fmt.Sprintf("%s/pulse.log", logPath), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err2 != nil {
log.WithFields(
log.Fields{
"block": "main",
"_module": "pulsed",
"error": err2.Error(),
"logpath": logPath,
}).Fatal("bad log path")
os.Exit(1)
}
defer file.Close()
log.Info("setting log path to: ", logPath)
log.SetOutput(file)
} else {
log.Info("setting log path to: stdout")
}

controlOpts := []control.ControlOpt{
control.MaxRunningPlugins(maxRunning),
control.CacheExpiration(cache),
Expand Down

0 comments on commit e6e0668

Please sign in to comment.