Skip to content

Commit 7d453a2

Browse files
committed
Support additional log level types for LogIF
1 parent b2dcdba commit 7d453a2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

logger/logger.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,39 @@ func (l *Logger) Debug(msg string, fields ...zap.Field) {
8080
l.zapper.Debug(msg, l.withCommon(fields...)...)
8181
}
8282

83+
// DebugIf logs a message at DebugLevel if the error is not nil
84+
func (l *Logger) DebugIf(err error, msg string, fields ...zap.Field) {
85+
if err != nil {
86+
l.zapper.Debug(msg, l.withCommon(append(fields, zap.Error(err))...)...)
87+
}
88+
}
89+
8390
// Info logs a message at InfoLevel. The message includes any fields passed
8491
// at the log site, as well as any fields accumulated on the logger.
8592
func (l *Logger) Info(msg string, fields ...zap.Field) {
8693
l.zapper.Info(msg, l.withCommon(fields...)...)
8794
}
8895

96+
// InfoIf logs a message at InfoLevel if the error is not nil
97+
func (l *Logger) InfoIf(err error, msg string, fields ...zap.Field) {
98+
if err != nil {
99+
l.zapper.Info(msg, l.withCommon(append(fields, zap.Error(err))...)...)
100+
}
101+
}
102+
89103
// Warn logs a message at WarnLevel. The message includes any fields passed
90104
// at the log site, as well as any fields accumulated on the logger.
91105
func (l *Logger) Warn(msg string, fields ...zap.Field) {
92106
l.zapper.Warn(msg, l.withCommon(fields...)...)
93107
}
94108

109+
// WarnIf logs a message at WarnLevel if the error is not nil
110+
func (l *Logger) WarnIf(err error, msg string, fields ...zap.Field) {
111+
if err != nil {
112+
l.zapper.Warn(msg, l.withCommon(append(fields, zap.Error(err))...)...)
113+
}
114+
}
115+
95116
// Error logs a message at ErrorLevel. The message includes any fields passed
96117
// at the log site, as well as any fields accumulated on the logger.
97118
func (l *Logger) Error(msg string, fields ...zap.Field) {

0 commit comments

Comments
 (0)