From b4d4f3629a7fe77e4ed08026650bf21fa6f098b8 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Mon, 11 Aug 2025 12:19:23 -0300 Subject: [PATCH] logp: enable go vet checks for printf directives Currently, misuse of printf directives in logger functions is not reported by go vet. Use a small trick to enable the x/tools/go/analysis/passes/printf analyzer on printf wrappers, allowing this check to catch format string issues. --- loader/loader.go | 2 +- logp/logger.go | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/loader/loader.go b/loader/loader.go index 1f1d3296..08921a66 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -62,7 +62,7 @@ func (l *Loader) Load(files []string) (*Config, error) { return nil, fmt.Errorf("cannot get configuration from '%s': %w", f, err) } inputsList = append(inputsList, inp...) - l.logger.Debugf("Loaded %s input(s) from configuration from %s", len(inp), f) + l.logger.Debugf("Loaded %d input(s) from configuration from %s", len(inp), f) } else { if err := merger.Add(cfg.access(), err); err != nil { return nil, fmt.Errorf("failed to merge configuration file '%s' to existing one: %w", f, err) diff --git a/logp/logger.go b/logp/logger.go index b98c81b0..4c7a7ab5 100644 --- a/logp/logger.go +++ b/logp/logger.go @@ -82,9 +82,6 @@ func NewDevelopmentLogger(selector string, options ...LogOption) (*Logger, error return nil, err } logger = logger.Named(selector) - if err != nil { - return nil, err - } return &Logger{logger, logger.Sugar(), make(map[string]struct{})}, nil } @@ -224,37 +221,58 @@ func (l *Logger) IsDebug() bool { // Debugf uses fmt.Sprintf to construct and log a message. func (l *Logger) Debugf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Debugf(format, args...) } // Infof uses fmt.Sprintf to log a templated message. func (l *Logger) Infof(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Infof(format, args...) } // Warnf uses fmt.Sprintf to log a templated message. func (l *Logger) Warnf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Warnf(format, args...) } // Errorf uses fmt.Sprintf to log a templated message. func (l *Logger) Errorf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Errorf(format, args...) } // Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit(1). func (l *Logger) Fatalf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Fatalf(format, args...) } // Panicf uses fmt.Sprintf to log a templated message, then panics. func (l *Logger) Panicf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.Panicf(format, args...) } // DPanicf uses fmt.Sprintf to log a templated message. In development, the // logger then panics. func (l *Logger) DPanicf(format string, args ...interface{}) { + if false { + _ = fmt.Sprintf(format, args...) // enable printf checking + } l.sugar.DPanicf(format, args...) }