github.com/elfadel/cilium@v1.6.12/test/config/logs.go (about)

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