github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/flogging/floggingtest/logger_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package floggingtest 8 9 import ( 10 "testing" 11 12 . "github.com/onsi/gomega" 13 "github.com/onsi/gomega/gbytes" 14 "go.uber.org/zap/zapcore" 15 ) 16 17 func TestLoggerRecorder(t *testing.T) { 18 gt := NewGomegaWithT(t) 19 20 tl, recorder := NewTestLogger(t, Named("test-logging"), AtLevel(zapcore.InfoLevel)) 21 tl.Error("this", "is", "an", "error") 22 23 gt.Expect(recorder.Entries()).To(HaveLen(1)) 24 gt.Expect(recorder.Entries()).To(ConsistOf("[test-logging] TestLoggerRecorder -> ERRO 0001 this is an error")) 25 26 gt.Expect(recorder.Messages()).To(HaveLen(1)) 27 gt.Expect(recorder.Messages()).To(ConsistOf("this is an error")) 28 29 gt.Expect(string(recorder.Buffer().Contents())).To(Equal("[test-logging] TestLoggerRecorder -> ERRO 0001 this is an error\n")) 30 gt.Expect(recorder).NotTo(gbytes.Say("nothing good")) 31 gt.Expect(recorder).To(gbytes.Say(`\Q[test-logging] TestLoggerRecorder -> ERRO 0001 this is an error\E`)) 32 } 33 34 func TestLoggerRecorderRegex(t *testing.T) { 35 gt := NewGomegaWithT(t) 36 37 tl, recorder := NewTestLogger(t, Named("test-logging")) 38 tl.Debug("message one") 39 tl.Debug("message two") 40 tl.Debug("message three") 41 42 gt.Expect(recorder.EntriesContaining("message")).To(HaveLen(3)) 43 gt.Expect(recorder.EntriesMatching("test-logging.*message t")).To(HaveLen(2)) 44 gt.Expect(recorder.MessagesContaining("message")).To(HaveLen(3)) 45 gt.Expect(recorder.MessagesMatching("^message t")).To(HaveLen(2)) 46 47 gt.Expect(recorder.EntriesContaining("one")).To(HaveLen(1)) 48 gt.Expect(recorder.MessagesContaining("one")).To(HaveLen(1)) 49 50 gt.Expect(recorder.EntriesContaining("two")).To(HaveLen(1)) 51 gt.Expect(recorder.MessagesContaining("two")).To(HaveLen(1)) 52 53 gt.Expect(recorder.EntriesContaining("")).To(HaveLen(3)) 54 gt.Expect(recorder.MessagesContaining("")).To(HaveLen(3)) 55 56 gt.Expect(recorder.EntriesContaining("mismatch")).To(HaveLen(0)) 57 gt.Expect(recorder.MessagesContaining("mismatch")).To(HaveLen(0)) 58 } 59 60 func TestRecorderReset(t *testing.T) { 61 gt := NewGomegaWithT(t) 62 63 tl, recorder := NewTestLogger(t, Named("test-logging")) 64 tl.Debug("message one") 65 tl.Debug("message two") 66 tl.Debug("message three") 67 68 gt.Expect(recorder.Entries()).To(HaveLen(3)) 69 gt.Expect(recorder.Messages()).To(HaveLen(3)) 70 gt.Expect(recorder.Buffer().Contents()).NotTo(BeEmpty()) 71 72 recorder.Reset() 73 gt.Expect(recorder.Entries()).To(HaveLen(0)) 74 gt.Expect(recorder.Messages()).To(HaveLen(0)) 75 gt.Expect(recorder.Buffer().Contents()).To(BeEmpty()) 76 } 77 78 func TestFatalAsPanic(t *testing.T) { 79 gt := NewGomegaWithT(t) 80 81 tl, _ := NewTestLogger(t) 82 gt.Expect(func() { tl.Fatal("this", "is", "an", "error") }).To(Panic()) 83 } 84 85 func TestRecordingCoreWith(t *testing.T) { 86 gt := NewGomegaWithT(t) 87 logger, recorder := NewTestLogger(t) 88 logger = logger.With("key", "value") 89 90 logger.Debug("message") 91 gt.Expect(recorder).To(gbytes.Say(`message key=value`)) 92 }