Skip to content
/ etcd Public
forked from etcd-io/etcd

Commit

Permalink
etcdmain: add log-output flag
Browse files Browse the repository at this point in the history
So we can choose where to write logs.
Fix etcd-io#5449.
  • Loading branch information
gyuho committed Jun 15, 2016
1 parent 7d666ab commit 4eb89c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type config struct {
// Debug logging
Debug bool `json:"debug"`
LogPkgLevels string `json:"log-package-levels"`
LogOutput string `json:"log-output"`

// ForceNewCluster is unsafe
ForceNewCluster bool `json:"force-new-cluster"`
Expand Down Expand Up @@ -249,6 +250,7 @@ func NewConfig() *config {
// logging
fs.BoolVar(&cfg.Debug, "debug", false, "Enable debug-level logging for etcd.")
fs.StringVar(&cfg.LogPkgLevels, "log-package-levels", "", "Specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').")
fs.StringVar(&cfg.LogOutput, "log-output", "stderr", "Specify log output path (stderr,stdout,journald,YOUR_NAME.log).")

// unsafe
fs.BoolVar(&cfg.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.")
Expand Down
37 changes: 37 additions & 0 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
_ "net/http/pprof"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -616,6 +617,42 @@ func setupLogging(cfg *config) {
}
repoLog.SetLogLevel(settings)
}
switch cfg.LogOutput {
case "", "stderr":
break

case "stdout":
capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug))

case "journald":
f, err := capnslog.NewJournaldFormatter()
if err != nil {
plog.Warningf("couldn't log to journald (%v)", err)
return
}
capnslog.SetFormatter(f)

default:
if filepath.Ext(cfg.LogOutput) == ".log" {
f, err := openToAppend(cfg.LogOutput)
if err != nil {
plog.Warningf("couldn't log to file %q (%v)", cfg.LogOutput, err)
return
}
capnslog.SetFormatter(capnslog.NewPrettyFormatter(f, cfg.Debug))
}
}
}

func openToAppend(fpath string) (*os.File, error) {
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_APPEND, fileutil.PrivateFileMode)
if err != nil {
f, err = os.Create(fpath)
if err != nil {
return f, err
}
}
return f, nil
}

func checkSupportArch() {
Expand Down
2 changes: 2 additions & 0 deletions etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ logging flags
enable debug-level logging for etcd.
--log-package-levels ''
specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').
--log-output 'stderr'
specify log output path (stderr,stdout,journald,YOUR_NAME.log).
unsafe flags:
Expand Down

0 comments on commit 4eb89c4

Please sign in to comment.