Skip to content

Commit b9435dd

Browse files
joeybloggsjoeybloggs
authored andcommitted
update syslog coverage to 100%
1 parent fd294e9 commit b9435dd

File tree

3 files changed

+329
-9
lines changed

3 files changed

+329
-9
lines changed

handlers/console/console.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ type Console struct {
2828
colors [9]log.ANSIEscSeq
2929
ansiReset log.ANSIEscSeq
3030
writer io.Writer
31-
miniTimestamp bool
3231
timestampFormat string
33-
displayColor bool
3432
start time.Time
3533
format string
3634
formatFields string
3735
formatKeyValue string
3836
formatTs func(e *log.Entry) string
37+
displayColor bool
38+
miniTimestamp bool
3939
}
4040

4141
// Colors mapping.

handlers/syslog/syslog.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type Syslog struct {
2727
buffer uint
2828
colors [9]log.ANSIEscSeq
2929
ansiReset log.ANSIEscSeq
30-
displayColor bool
3130
writer *syslog.Writer
32-
hasCustomFormatter bool
3331
formatter Formatter
3432
timestampFormat string
3533
format string
3634
formatKeyValue string
35+
displayColor bool
36+
hasCustomFormatter bool
3737
}
3838

3939
var (
@@ -230,7 +230,7 @@ func (s *Syslog) defaultFormatEntryCaller(e *log.Entry) string {
230230
}
231231
}
232232

233-
fmt.Fprintf(buff, s.format, e.Timestamp.Format(s.timestampFormat), e.Level, e.Message)
233+
fmt.Fprintf(buff, s.format, e.Timestamp.Format(s.timestampFormat), e.Level, file, e.Line, e.Message)
234234

235235
for _, f := range e.Fields {
236236
fmt.Fprintf(buff, s.formatKeyValue, f.Key, f.Value)

handlers/syslog/syslog_test.go

Lines changed: 324 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func hasString(conn *net.UDPConn, s string) bool {
3737
return strings.Contains(read, s)
3838
}
3939

40+
func TestBadAddress(t *testing.T) {
41+
sLog, err := New("udp", "255.255.255.67", stdsyslog.LOG_DEBUG, "")
42+
Equal(t, sLog, nil)
43+
NotEqual(t, err, nil)
44+
}
45+
4046
func TestSyslogLogger(t *testing.T) {
4147

4248
year := time.Now().Format("2006")
@@ -356,8 +362,322 @@ func TestSyslogLoggerColor(t *testing.T) {
356362
Equal(t, hasString(conn, "2016  DEBUG debug"), true)
357363
}
358364

359-
func TestBadAddress(t *testing.T) {
360-
sLog, err := New("udp", "255.255.255.67", stdsyslog.LOG_DEBUG, "")
361-
Equal(t, sLog, nil)
362-
NotEqual(t, err, nil)
365+
func TestSyslogLoggerCaller(t *testing.T) {
366+
367+
year := time.Now().Format("2006")
368+
369+
addr, err := net.ResolveUDPAddr("udp", ":2002")
370+
Equal(t, err, nil)
371+
372+
conn, err := net.ListenUDP("udp", addr)
373+
Equal(t, err, nil)
374+
defer conn.Close()
375+
376+
sLog, err := New("udp", "127.0.0.1:2002", stdsyslog.LOG_DEBUG, "")
377+
Equal(t, err, nil)
378+
379+
sLog.DisplayColor(false)
380+
sLog.SetChannelBuffer(3)
381+
sLog.SetTimestampFormat("2006")
382+
sLog.SetANSIReset(log.Reset)
383+
384+
log.SetCallerInfo(true)
385+
log.RegisterHandler(sLog, log.AllLevels...)
386+
387+
log.Debug("debug")
388+
Equal(t, hasString(conn, year+" DEBUG syslog_test.go:387 debug"), true)
389+
390+
log.Debugf("%s", "debugf")
391+
Equal(t, hasString(conn, year+" DEBUG syslog_test.go:390 debugf"), true)
392+
393+
log.Info("info")
394+
Equal(t, hasString(conn, year+" INFO syslog_test.go:393 info"), true)
395+
396+
log.Infof("%s", "infof")
397+
Equal(t, hasString(conn, year+" INFO syslog_test.go:396 infof"), true)
398+
399+
log.Notice("notice")
400+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:399 notice"), true)
401+
402+
log.Noticef("%s", "noticef")
403+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:402 noticef"), true)
404+
405+
log.Warn("warn")
406+
Equal(t, hasString(conn, year+" WARN syslog_test.go:405 warn"), true)
407+
408+
log.Warnf("%s", "warnf")
409+
Equal(t, hasString(conn, year+" WARN syslog_test.go:408 warnf"), true)
410+
411+
log.Error("error")
412+
Equal(t, hasString(conn, year+" ERROR syslog_test.go:411 error"), true)
413+
414+
log.Errorf("%s", "errorf")
415+
Equal(t, hasString(conn, year+" ERROR syslog_test.go:414 errorf"), true)
416+
417+
log.Alert("alert")
418+
Equal(t, hasString(conn, year+" ALERT syslog_test.go:417 alert"), true)
419+
420+
log.Alertf("%s", "alertf")
421+
Equal(t, hasString(conn, year+" ALERT syslog_test.go:420 alertf"), true)
422+
423+
log.Print("print")
424+
Equal(t, hasString(conn, year+" INFO syslog_test.go:423 print"), true)
425+
426+
log.Printf("%s", "printf")
427+
Equal(t, hasString(conn, year+" INFO syslog_test.go:426 printf"), true)
428+
429+
log.Println("println")
430+
Equal(t, hasString(conn, year+" INFO syslog_test.go:429 println"), true)
431+
432+
PanicMatches(t, func() { log.Panic("panic") }, "panic")
433+
Equal(t, hasString(conn, year+" PANIC syslog_test.go:432 panic"), true)
434+
435+
PanicMatches(t, func() { log.Panicf("%s", "panicf") }, "panicf")
436+
Equal(t, hasString(conn, year+" PANIC syslog_test.go:435 panicf"), true)
437+
438+
PanicMatches(t, func() { log.Panicln("panicln") }, "panicln")
439+
Equal(t, hasString(conn, year+" PANIC syslog_test.go:438 panicln"), true)
440+
441+
// WithFields
442+
log.WithFields(log.F("key", "value")).Debug("debug")
443+
Equal(t, hasString(conn, year+" DEBUG syslog_test.go:442 debug key=value"), true)
444+
445+
log.WithFields(log.F("key", "value")).Debugf("%s", "debugf")
446+
Equal(t, hasString(conn, year+" DEBUG syslog_test.go:445 debugf key=value"), true)
447+
448+
log.WithFields(log.F("key", "value")).Info("info")
449+
Equal(t, hasString(conn, year+" INFO syslog_test.go:448 info key=value"), true)
450+
451+
log.WithFields(log.F("key", "value")).Infof("%s", "infof")
452+
Equal(t, hasString(conn, year+" INFO syslog_test.go:451 infof key=value"), true)
453+
454+
log.WithFields(log.F("key", "value")).Notice("notice")
455+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:454 notice key=value"), true)
456+
457+
log.WithFields(log.F("key", "value")).Noticef("%s", "noticef")
458+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:457 noticef key=value"), true)
459+
460+
log.WithFields(log.F("key", "value")).Warn("warn")
461+
Equal(t, hasString(conn, year+" WARN syslog_test.go:460 warn key=value"), true)
462+
463+
log.WithFields(log.F("key", "value")).Warnf("%s", "warnf")
464+
Equal(t, hasString(conn, year+" WARN syslog_test.go:463 warnf key=value"), true)
465+
466+
log.WithFields(log.F("key", "value")).Error("error")
467+
Equal(t, hasString(conn, year+" ERROR syslog_test.go:466 error key=value"), true)
468+
469+
log.WithFields(log.F("key", "value")).Errorf("%s", "errorf")
470+
Equal(t, hasString(conn, year+" ERROR syslog_test.go:469 errorf key=value"), true)
471+
472+
log.WithFields(log.F("key", "value")).Alert("alert")
473+
Equal(t, hasString(conn, year+" ALERT syslog_test.go:472 alert key=value"), true)
474+
475+
log.WithFields(log.F("key", "value")).Alertf("%s", "alertf")
476+
Equal(t, hasString(conn, year+" ALERT syslog_test.go:475 alertf key=value"), true)
477+
478+
PanicMatches(t, func() { log.WithFields(log.F("key", "value")).Panicf("%s", "panicf") }, "panicf key=value")
479+
Equal(t, hasString(conn, year+" PANIC syslog_test.go:478 panicf key=value"), true)
480+
481+
PanicMatches(t, func() { log.WithFields(log.F("key", "value")).Panic("panic") }, "panic key=value")
482+
Equal(t, hasString(conn, year+" PANIC syslog_test.go:481 panic key=value"), true)
483+
484+
func() {
485+
defer log.Trace("trace").End()
486+
}()
487+
488+
Equal(t, hasString(conn, year+" TRACE syslog_test.go:486 trace"), true)
489+
490+
func() {
491+
defer log.Tracef("tracef").End()
492+
}()
493+
494+
Equal(t, hasString(conn, year+" TRACE syslog_test.go:492 tracef"), true)
495+
496+
func() {
497+
defer log.WithFields(log.F("key", "value")).Trace("trace").End()
498+
}()
499+
500+
Equal(t, hasString(conn, year+" TRACE syslog_test.go:498 trace"), true)
501+
502+
func() {
503+
defer log.WithFields(log.F("key", "value")).Tracef("tracef").End()
504+
}()
505+
506+
Equal(t, hasString(conn, year+" TRACE syslog_test.go:504 tracef"), true)
507+
508+
e := &log.Entry{
509+
Level: log.FatalLevel,
510+
Message: "fatal",
511+
Timestamp: time.Now(),
512+
}
513+
514+
log.HandleEntry(e)
515+
516+
Equal(t, hasString(conn, year+" FATAL :0 fatal"), true)
517+
518+
// Test Custom Formatter
519+
sLog.SetFormatter(func(e *log.Entry) string {
520+
return e.Message
521+
})
522+
523+
log.Debug("debug")
524+
Equal(t, hasString(conn, "debug"), true)
525+
}
526+
527+
func TestSyslogLoggerColorCaller(t *testing.T) {
528+
529+
year := time.Now().Format("2006")
530+
531+
addr, err := net.ResolveUDPAddr("udp", ":2003")
532+
Equal(t, err, nil)
533+
534+
conn, err := net.ListenUDP("udp", addr)
535+
Equal(t, err, nil)
536+
defer conn.Close()
537+
538+
sLog, err := New("udp", "127.0.0.1:2003", stdsyslog.LOG_DEBUG, "")
539+
Equal(t, err, nil)
540+
541+
sLog.DisplayColor(true)
542+
sLog.SetChannelBuffer(3)
543+
sLog.SetTimestampFormat("2006")
544+
545+
log.RegisterHandler(sLog, log.AllLevels...)
546+
547+
log.Debug("debug")
548+
Equal(t, hasString(conn, year+"  DEBUG syslog_test.go:547 debug"), true)
549+
550+
log.Debugf("%s", "debugf")
551+
Equal(t, hasString(conn, year+"  DEBUG syslog_test.go:550 debugf"), true)
552+
553+
log.Info("info")
554+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:553 info"), true)
555+
556+
log.Infof("%s", "infof")
557+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:556 info"), true)
558+
559+
log.Notice("notice")
560+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:559 notice"), true)
561+
562+
log.Noticef("%s", "noticef")
563+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:562 noticef"), true)
564+
565+
log.Warn("warn")
566+
Equal(t, hasString(conn, year+"  WARN syslog_test.go:565 warn"), true)
567+
568+
log.Warnf("%s", "warnf")
569+
Equal(t, hasString(conn, year+"  WARN syslog_test.go:568 warnf"), true)
570+
571+
log.Error("error")
572+
Equal(t, hasString(conn, year+"  ERROR syslog_test.go:571 error"), true)
573+
574+
log.Errorf("%s", "errorf")
575+
Equal(t, hasString(conn, year+"  ERROR syslog_test.go:574 errorf"), true)
576+
577+
log.Alert("alert")
578+
Equal(t, hasString(conn, year+"  ALERT syslog_test.go:577 alert"), true)
579+
580+
log.Alertf("%s", "alertf")
581+
Equal(t, hasString(conn, year+"  ALERT syslog_test.go:580 alertf"), true)
582+
583+
log.Print("print")
584+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:583 print"), true)
585+
586+
log.Printf("%s", "printf")
587+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:586 printf"), true)
588+
589+
log.Println("println")
590+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:589 println"), true)
591+
592+
PanicMatches(t, func() { log.Panic("panic") }, "panic")
593+
Equal(t, hasString(conn, year+"  PANIC syslog_test.go:592 panic"), true)
594+
595+
PanicMatches(t, func() { log.Panicf("%s", "panicf") }, "panicf")
596+
Equal(t, hasString(conn, year+"  PANIC syslog_test.go:595 panicf"), true)
597+
598+
PanicMatches(t, func() { log.Panicln("panicln") }, "panicln")
599+
Equal(t, hasString(conn, year+"  PANIC syslog_test.go:598 panicln"), true)
600+
601+
// WithFields
602+
log.WithFields(log.F("key", "value")).Debug("debug")
603+
Equal(t, hasString(conn, year+"  DEBUG syslog_test.go:602 debug key=value"), true)
604+
605+
log.WithFields(log.F("key", "value")).Debugf("%s", "debugf")
606+
Equal(t, hasString(conn, year+"  DEBUG syslog_test.go:605 debugf key=value"), true)
607+
608+
log.WithFields(log.F("key", "value")).Info("info")
609+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:608 info key=value"), true)
610+
611+
log.WithFields(log.F("key", "value")).Infof("%s", "infof")
612+
Equal(t, hasString(conn, year+"  INFO syslog_test.go:611 infof key=value"), true)
613+
614+
log.WithFields(log.F("key", "value")).Notice("notice")
615+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:614 notice key=value"), true)
616+
617+
log.WithFields(log.F("key", "value")).Noticef("%s", "noticef")
618+
Equal(t, hasString(conn, year+" NOTICE syslog_test.go:617 noticef key=value"), true)
619+
620+
log.WithFields(log.F("key", "value")).Warn("warn")
621+
Equal(t, hasString(conn, year+"  WARN syslog_test.go:620 warn key=value"), true)
622+
623+
log.WithFields(log.F("key", "value")).Warnf("%s", "warnf")
624+
Equal(t, hasString(conn, year+"  WARN syslog_test.go:623 warnf key=value"), true)
625+
626+
log.WithFields(log.F("key", "value")).Error("error")
627+
Equal(t, hasString(conn, year+"  ERROR syslog_test.go:626 error key=value"), true)
628+
629+
log.WithFields(log.F("key", "value")).Errorf("%s", "errorf")
630+
Equal(t, hasString(conn, year+"  ERROR syslog_test.go:629 errorf key=value"), true)
631+
632+
log.WithFields(log.F("key", "value")).Alert("alert")
633+
Equal(t, hasString(conn, year+"  ALERT syslog_test.go:632 alert key=value"), true)
634+
635+
log.WithFields(log.F("key", "value")).Alertf("%s", "alertf")
636+
Equal(t, hasString(conn, year+"  ALERT syslog_test.go:635 alertf key=value"), true)
637+
638+
PanicMatches(t, func() { log.WithFields(log.F("key", "value")).Panicf("%s", "panicf") }, "panicf key=value")
639+
Equal(t, hasString(conn, year+"  PANIC syslog_test.go:638 panicf key=value"), true)
640+
641+
PanicMatches(t, func() { log.WithFields(log.F("key", "value")).Panic("panic") }, "panic key=value")
642+
Equal(t, hasString(conn, year+"  PANIC syslog_test.go:641 panic key=value"), true)
643+
644+
func() {
645+
defer log.Trace("trace").End()
646+
}()
647+
648+
Equal(t, hasString(conn, year+"  TRACE syslog_test.go:646 trace"), true)
649+
650+
func() {
651+
defer log.Tracef("tracef").End()
652+
}()
653+
654+
Equal(t, hasString(conn, year+"  TRACE syslog_test.go:652 tracef"), true)
655+
656+
func() {
657+
defer log.WithFields(log.F("key", "value")).Trace("trace").End()
658+
}()
659+
660+
Equal(t, hasString(conn, year+"  TRACE syslog_test.go:658 trace"), true)
661+
662+
func() {
663+
defer log.WithFields(log.F("key", "value")).Tracef("tracef").End()
664+
}()
665+
666+
Equal(t, hasString(conn, year+"  TRACE syslog_test.go:664 tracef"), true)
667+
668+
e := &log.Entry{
669+
Level: log.FatalLevel,
670+
Message: "fatal",
671+
Timestamp: time.Now(),
672+
}
673+
674+
log.HandleEntry(e)
675+
676+
Equal(t, hasString(conn, year+"  FATAL :0 fatal"), true)
677+
678+
// test changing level color
679+
sLog.SetLevelColor(log.DebugLevel, log.Red)
680+
681+
log.Debug("debug")
682+
Equal(t, hasString(conn, "2016  DEBUG syslog_test.go:681 debug"), true)
363683
}

0 commit comments

Comments
 (0)