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

fix: Add stdout validation when setting logging output path #666

Merged
merged 2 commits into from
Jul 26, 2022
Merged
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
9 changes: 5 additions & 4 deletions logging/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewEncoderFormatOption(v EncoderFormat) EncoderFormatOption {

const (
stderr = "stderr"
stdout = "stdout"

JSON EncoderFormat = iota
CSV
Expand Down Expand Up @@ -238,12 +239,12 @@ func (oldConfig Config) with(newConfigOptions Config) Config {
func validatePaths(paths []string) []string {
validatedPaths := make([]string, 0, len(paths))
for _, p := range paths {
if p == stderr {
if p == stderr || p == stdout {
validatedPaths = append(validatedPaths, p)
continue
}

if f, err := os.OpenFile(p, os.O_CREATE|os.O_APPEND, 0666); err != nil {
if f, err := os.OpenFile(p, os.O_CREATE|os.O_APPEND, 0644); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@orpheuslummis I hope this makes you happy.

Copy link
Contributor

Choose a reason for hiding this comment

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

making the codebase accessible even to the superstitious

Copy link
Member

Choose a reason for hiding this comment

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

WOW and this is PR # 666

Copy link
Member

Choose a reason for hiding this comment

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

Illuminati confirmed?

Copy link
Contributor

Choose a reason for hiding this comment

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

can't escape 666

Copy link
Member

Choose a reason for hiding this comment

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

  • queue X-Files theme*

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is hilarious!!!!!

Copy link
Member

Choose a reason for hiding this comment

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

  • queue X-Files theme

This played in my mind while I read this.

log.Info(context.Background(), "cannot use provided path", NewKV("err", err))
} else {
err := f.Close()
Expand All @@ -258,12 +259,12 @@ func validatePaths(paths []string) []string {
return validatedPaths
}

func willOutputToStderr(paths []string) bool {
func willOutputToStderrOrStdout(paths []string) bool {
if len(paths) == 0 {
return true
}
for _, p := range paths {
if p == stderr {
if p == stderr || p == stdout {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (l *logger) ApplyConfig(config Config) {
_ = l.logger.Sync()
l.logger = newLogger

if !willOutputToStderr(config.OutputPaths) {
if !willOutputToStderrOrStdout(config.OutputPaths) {
if config.pipe != nil { // for testing purposes only
l.consoleLogger = stdlog.New(config.pipe, "", 0)
} else {
Expand Down Expand Up @@ -209,7 +209,7 @@ func buildZapLogger(name string, config Config) (*zap.Logger, error) {
return nil, err
}

if willOutputToStderr(defaultConfig.OutputPaths) && config.pipe != nil {
if willOutputToStderrOrStdout(defaultConfig.OutputPaths) && config.pipe != nil {
newLogger = newLogger.WithOptions(zap.WrapCore(func(zapcore.Core) zapcore.Core {
cfg := zap.NewProductionEncoderConfig()
cfg.ConsoleSeparator = defaultConfig.EncoderConfig.ConsoleSeparator
Expand Down