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

etcdmain: add log-output flag #5678

Closed
wants to merge 1 commit 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
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
31 changes: 31 additions & 0 deletions etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,37 @@ func setupLogging(cfg *config) {
}
repoLog.SetLogLevel(settings)
}
switch cfg.LogOutput {
case "", "stderr":
Copy link
Contributor

Choose a reason for hiding this comment

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

does journald auto detection still work?

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:
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|os.O_CREATE, fileutil.PrivateFileMode)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed if using os.O_CREATE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok will fix. thanks

return nil, 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