Skip to content

Commit 59f6c9d

Browse files
committed
Add *ln variants of methods to the SugaredLogger
1 parent 1d435ba commit 59f6c9d

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

sugar.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,48 @@ func (s *SugaredLogger) Fatalw(msg string, keysAndValues ...interface{}) {
220220
s.log(FatalLevel, msg, nil, keysAndValues)
221221
}
222222

223+
// Debugln uses fmt.Sprintln to construct and log a message.
224+
func (s *SugaredLogger) Debugln(args ...interface{}) {
225+
s.logln(DebugLevel, "", args, nil)
226+
}
227+
228+
// Infoln uses fmt.Sprintln to construct and log a message.
229+
func (s *SugaredLogger) Infoln(args ...interface{}) {
230+
s.logln(InfoLevel, "", args, nil)
231+
}
232+
233+
// Warnln uses fmt.Sprintln to construct and log a message.
234+
func (s *SugaredLogger) Warnln(args ...interface{}) {
235+
s.logln(WarnLevel, "", args, nil)
236+
}
237+
238+
// Errorln uses fmt.Sprintln to construct and log a message.
239+
func (s *SugaredLogger) Errorln(args ...interface{}) {
240+
s.logln(ErrorLevel, "", args, nil)
241+
}
242+
243+
// DPanicln uses fmt.Sprintln to construct and log a message. In development, the
244+
// logger then panics. (See DPanicLevel for details.)
245+
func (s *SugaredLogger) DPanicln(args ...interface{}) {
246+
s.logln(DPanicLevel, "", args, nil)
247+
}
248+
249+
// Panicln uses fmt.Sprintln to construct and log a message, then panics.
250+
func (s *SugaredLogger) Panicln(args ...interface{}) {
251+
s.logln(PanicLevel, "", args, nil)
252+
}
253+
254+
// Fatalln uses fmt.Sprintln to construct and log a message, then calls os.Exit.
255+
func (s *SugaredLogger) Fatalln(args ...interface{}) {
256+
s.logln(FatalLevel, "", args, nil)
257+
}
258+
223259
// Sync flushes any buffered log entries.
224260
func (s *SugaredLogger) Sync() error {
225261
return s.base.Sync()
226262
}
227263

264+
// log message with Sprint, Sprintf, or neither.
228265
func (s *SugaredLogger) log(lvl zapcore.Level, template string, fmtArgs []interface{}, context []interface{}) {
229266
// If logging at this level is completely disabled, skip the overhead of
230267
// string formatting.
@@ -238,6 +275,18 @@ func (s *SugaredLogger) log(lvl zapcore.Level, template string, fmtArgs []interf
238275
}
239276
}
240277

278+
// logln message with Sprintln
279+
func (s *SugaredLogger) logln(lvl zapcore.Level, template string, fmtArgs []interface{}, context []interface{}) {
280+
if lvl < DPanicLevel && !s.base.Core().Enabled(lvl) {
281+
return
282+
}
283+
284+
msg := getMessageln(fmtArgs)
285+
if ce := s.base.Check(lvl, msg); ce != nil {
286+
ce.Write(s.sweetenFields(context)...)
287+
}
288+
}
289+
241290
// getMessage format with Sprint, Sprintf, or neither.
242291
func getMessage(template string, fmtArgs []interface{}) string {
243292
if len(fmtArgs) == 0 {
@@ -256,6 +305,12 @@ func getMessage(template string, fmtArgs []interface{}) string {
256305
return fmt.Sprint(fmtArgs...)
257306
}
258307

308+
// getMessageln format with Sprintln.
309+
func getMessageln(fmtArgs []interface{}) string {
310+
msg := fmt.Sprintln(fmtArgs...)
311+
return msg[:len(msg)-1]
312+
}
313+
259314
func (s *SugaredLogger) sweetenFields(args []interface{}) []Field {
260315
if len(args) == 0 {
261316
return nil

sugar_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,42 @@ func TestSugarTemplatedLogging(t *testing.T) {
261261
}
262262
}
263263

264+
func TestSugarLnLogging(t *testing.T) {
265+
tests := []struct {
266+
args []interface{}
267+
expect string
268+
}{
269+
{nil, ""},
270+
{[]interface{}{}, ""},
271+
{[]interface{}{""}, ""},
272+
{[]interface{}{"foo"}, "foo"},
273+
{[]interface{}{"foo", "bar"}, "foo bar"},
274+
}
275+
276+
// Common to all test cases.
277+
context := []interface{}{"foo", "bar"}
278+
expectedFields := []Field{String("foo", "bar")}
279+
280+
for _, tt := range tests {
281+
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
282+
logger.With(context...).Debugln(tt.args...)
283+
logger.With(context...).Infoln(tt.args...)
284+
logger.With(context...).Warnln(tt.args...)
285+
logger.With(context...).Errorln(tt.args...)
286+
logger.With(context...).DPanicln(tt.args...)
287+
288+
expected := make([]observer.LoggedEntry, 5)
289+
for i, lvl := range []zapcore.Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel} {
290+
expected[i] = observer.LoggedEntry{
291+
Entry: zapcore.Entry{Message: tt.expect, Level: lvl},
292+
Context: expectedFields,
293+
}
294+
}
295+
assert.Equal(t, expected, logs.AllUntimed(), "Unexpected log output.")
296+
})
297+
}
298+
}
299+
264300
func TestSugarPanicLogging(t *testing.T) {
265301
tests := []struct {
266302
loggerLevel zapcore.Level
@@ -276,6 +312,9 @@ func TestSugarPanicLogging(t *testing.T) {
276312
{FatalLevel, func(s *SugaredLogger) { s.Panicw("foo") }, ""},
277313
{PanicLevel, func(s *SugaredLogger) { s.Panicw("foo") }, "foo"},
278314
{DebugLevel, func(s *SugaredLogger) { s.Panicw("foo") }, "foo"},
315+
{FatalLevel, func(s *SugaredLogger) { s.Panicln("foo") }, ""},
316+
{PanicLevel, func(s *SugaredLogger) { s.Panicln("foo") }, "foo"},
317+
{DebugLevel, func(s *SugaredLogger) { s.Panicln("foo") }, "foo"},
279318
}
280319

281320
for _, tt := range tests {
@@ -308,6 +347,9 @@ func TestSugarFatalLogging(t *testing.T) {
308347
{FatalLevel + 1, func(s *SugaredLogger) { s.Fatalw("foo") }, ""},
309348
{FatalLevel, func(s *SugaredLogger) { s.Fatalw("foo") }, "foo"},
310349
{DebugLevel, func(s *SugaredLogger) { s.Fatalw("foo") }, "foo"},
350+
{FatalLevel + 1, func(s *SugaredLogger) { s.Fatalln("foo") }, ""},
351+
{FatalLevel, func(s *SugaredLogger) { s.Fatalln("foo") }, "foo"},
352+
{DebugLevel, func(s *SugaredLogger) { s.Fatalln("foo") }, "foo"},
311353
}
312354

313355
for _, tt := range tests {
@@ -385,10 +427,34 @@ func TestSugarWithOptionsIncreaseLevel(t *testing.T) {
385427
})
386428
}
387429

430+
func TestSugarLnWithOptionsIncreaseLevel(t *testing.T) {
431+
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
432+
logger = logger.WithOptions(IncreaseLevel(WarnLevel))
433+
logger.Infoln("logger.Infoln")
434+
logger.Warnln("logger.Warnln")
435+
logger.Errorln("logger.Errorln")
436+
require.Equal(t, 2, logs.Len(), "expected only warn + error logs due to IncreaseLevel.")
437+
assert.Equal(
438+
t,
439+
logs.AllUntimed()[0].Message,
440+
"logger.Warnln",
441+
"Expected first logged message to be warn level message",
442+
)
443+
})
444+
}
445+
388446
func BenchmarkSugarSingleStrArg(b *testing.B) {
389447
withSugar(b, InfoLevel, nil /* opts* */, func(log *SugaredLogger, logs *observer.ObservedLogs) {
390448
for i := 0; i < b.N; i++ {
391449
log.Info("hello world")
392450
}
393451
})
394452
}
453+
454+
func BenchmarkSugarLnSingleStrArg(b *testing.B) {
455+
withSugar(b, InfoLevel, nil /* opts* */, func(log *SugaredLogger, logs *observer.ObservedLogs) {
456+
for i := 0; i < b.N; i++ {
457+
log.Infoln("hello world")
458+
}
459+
})
460+
}

0 commit comments

Comments
 (0)