Skip to content

Commit

Permalink
fix: Add stdout validation when setting logging output path (sourcene…
Browse files Browse the repository at this point in the history
…twork#666)

Relevant issue(s)
Resolves sourcenetwork#665

Description
This PR fixes the outputPath validation for the logging package. It ensures an stdout file isn't created during validation.
  • Loading branch information
fredcarle authored Jul 26, 2022
1 parent f902128 commit edad0ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
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 {
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

0 comments on commit edad0ee

Please sign in to comment.