github.com/cilium/cilium@v1.16.2/test/logger/logs.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package logger 5 6 import ( 7 "bytes" 8 "fmt" 9 "os" 10 11 "github.com/sirupsen/logrus" 12 13 ginkgo "github.com/cilium/cilium/test/ginkgo-ext" 14 ) 15 16 var ( 17 // Formatter is the format configuration to write logs into text 18 Formatter = logrus.TextFormatter{ 19 DisableTimestamp: false, 20 } 21 22 // TestLogWriter is a buffer in which all logs generated by a test are 23 // stored 24 TestLogWriter bytes.Buffer 25 // TestLogFileName is the file name to dump `TestLogWriter` content when 26 // test finish 27 TestLogFileName = "test-output.log" 28 ) 29 30 // TestLogWriterReset resets the current buffer 31 func TestLogWriterReset() { 32 TestLogWriter.Reset() 33 } 34 35 // LogHook to send logs via `ginkgo.GinkgoWriter`. 36 type LogHook struct{} 37 38 // Levels defined levels to send logs to FireAction 39 func (h *LogHook) Levels() []logrus.Level { 40 return []logrus.Level{ 41 logrus.WarnLevel, 42 logrus.ErrorLevel, 43 logrus.FatalLevel, 44 logrus.PanicLevel, 45 } 46 } 47 48 // Fire is a callback function used by logrus to write logs that match in 49 // the given by `Levels` method 50 func (h *LogHook) Fire(entry *logrus.Entry) (err error) { 51 line, err := Formatter.Format(entry) 52 if err == nil { 53 fmt.Fprint(ginkgo.GinkgoWriter, string(line)) 54 } else { 55 fmt.Fprintf(os.Stderr, "LogHook.Fire: unable to format log entry (%v)", err) 56 } 57 return 58 }