Skip to content

Commit e412635

Browse files
authored
logp: enable go vet checks for printf directives (#340)
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.
1 parent d5a250f commit e412635

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

loader/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (l *Loader) Load(files []string) (*Config, error) {
6262
return nil, fmt.Errorf("cannot get configuration from '%s': %w", f, err)
6363
}
6464
inputsList = append(inputsList, inp...)
65-
l.logger.Debugf("Loaded %s input(s) from configuration from %s", len(inp), f)
65+
l.logger.Debugf("Loaded %d input(s) from configuration from %s", len(inp), f)
6666
} else {
6767
if err := merger.Add(cfg.access(), err); err != nil {
6868
return nil, fmt.Errorf("failed to merge configuration file '%s' to existing one: %w", f, err)

logp/logger.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ func NewDevelopmentLogger(selector string, options ...LogOption) (*Logger, error
8282
return nil, err
8383
}
8484
logger = logger.Named(selector)
85-
if err != nil {
86-
return nil, err
87-
}
8885
return &Logger{logger, logger.Sugar(), make(map[string]struct{})}, nil
8986
}
9087

@@ -224,37 +221,58 @@ func (l *Logger) IsDebug() bool {
224221

225222
// Debugf uses fmt.Sprintf to construct and log a message.
226223
func (l *Logger) Debugf(format string, args ...interface{}) {
224+
if false {
225+
_ = fmt.Sprintf(format, args...) // enable printf checking
226+
}
227227
l.sugar.Debugf(format, args...)
228228
}
229229

230230
// Infof uses fmt.Sprintf to log a templated message.
231231
func (l *Logger) Infof(format string, args ...interface{}) {
232+
if false {
233+
_ = fmt.Sprintf(format, args...) // enable printf checking
234+
}
232235
l.sugar.Infof(format, args...)
233236
}
234237

235238
// Warnf uses fmt.Sprintf to log a templated message.
236239
func (l *Logger) Warnf(format string, args ...interface{}) {
240+
if false {
241+
_ = fmt.Sprintf(format, args...) // enable printf checking
242+
}
237243
l.sugar.Warnf(format, args...)
238244
}
239245

240246
// Errorf uses fmt.Sprintf to log a templated message.
241247
func (l *Logger) Errorf(format string, args ...interface{}) {
248+
if false {
249+
_ = fmt.Sprintf(format, args...) // enable printf checking
250+
}
242251
l.sugar.Errorf(format, args...)
243252
}
244253

245254
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit(1).
246255
func (l *Logger) Fatalf(format string, args ...interface{}) {
256+
if false {
257+
_ = fmt.Sprintf(format, args...) // enable printf checking
258+
}
247259
l.sugar.Fatalf(format, args...)
248260
}
249261

250262
// Panicf uses fmt.Sprintf to log a templated message, then panics.
251263
func (l *Logger) Panicf(format string, args ...interface{}) {
264+
if false {
265+
_ = fmt.Sprintf(format, args...) // enable printf checking
266+
}
252267
l.sugar.Panicf(format, args...)
253268
}
254269

255270
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
256271
// logger then panics.
257272
func (l *Logger) DPanicf(format string, args ...interface{}) {
273+
if false {
274+
_ = fmt.Sprintf(format, args...) // enable printf checking
275+
}
258276
l.sugar.DPanicf(format, args...)
259277
}
260278

0 commit comments

Comments
 (0)