github.com/MerlinKodo/quic-go@v0.39.2/internal/utils/log_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bytes"
     5  	"log"
     6  	"os"
     7  	"time"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Log", func() {
    14  	var b *bytes.Buffer
    15  
    16  	BeforeEach(func() {
    17  		b = &bytes.Buffer{}
    18  		log.SetOutput(b)
    19  	})
    20  
    21  	AfterEach(func() {
    22  		log.SetOutput(os.Stdout)
    23  		DefaultLogger.SetLogLevel(LogLevelNothing)
    24  	})
    25  
    26  	It("the log level has the correct numeric value", func() {
    27  		Expect(LogLevelNothing).To(BeEquivalentTo(0))
    28  		Expect(LogLevelError).To(BeEquivalentTo(1))
    29  		Expect(LogLevelInfo).To(BeEquivalentTo(2))
    30  		Expect(LogLevelDebug).To(BeEquivalentTo(3))
    31  	})
    32  
    33  	It("log level nothing", func() {
    34  		DefaultLogger.SetLogLevel(LogLevelNothing)
    35  		DefaultLogger.Debugf("debug")
    36  		DefaultLogger.Infof("info")
    37  		DefaultLogger.Errorf("err")
    38  		Expect(b.String()).To(BeEmpty())
    39  	})
    40  
    41  	It("log level err", func() {
    42  		DefaultLogger.SetLogLevel(LogLevelError)
    43  		DefaultLogger.Debugf("debug")
    44  		DefaultLogger.Infof("info")
    45  		DefaultLogger.Errorf("err")
    46  		Expect(b.String()).To(ContainSubstring("err\n"))
    47  		Expect(b.String()).ToNot(ContainSubstring("info"))
    48  		Expect(b.String()).ToNot(ContainSubstring("debug"))
    49  	})
    50  
    51  	It("log level info", func() {
    52  		DefaultLogger.SetLogLevel(LogLevelInfo)
    53  		DefaultLogger.Debugf("debug")
    54  		DefaultLogger.Infof("info")
    55  		DefaultLogger.Errorf("err")
    56  		Expect(b.String()).To(ContainSubstring("err\n"))
    57  		Expect(b.String()).To(ContainSubstring("info\n"))
    58  		Expect(b.String()).ToNot(ContainSubstring("debug"))
    59  	})
    60  
    61  	It("log level debug", func() {
    62  		DefaultLogger.SetLogLevel(LogLevelDebug)
    63  		DefaultLogger.Debugf("debug")
    64  		DefaultLogger.Infof("info")
    65  		DefaultLogger.Errorf("err")
    66  		Expect(b.String()).To(ContainSubstring("err\n"))
    67  		Expect(b.String()).To(ContainSubstring("info\n"))
    68  		Expect(b.String()).To(ContainSubstring("debug\n"))
    69  	})
    70  
    71  	It("doesn't add a timestamp if the time format is empty", func() {
    72  		DefaultLogger.SetLogLevel(LogLevelDebug)
    73  		DefaultLogger.SetLogTimeFormat("")
    74  		DefaultLogger.Debugf("debug")
    75  		Expect(b.String()).To(Equal("debug\n"))
    76  	})
    77  
    78  	It("adds a timestamp", func() {
    79  		format := "Jan 2, 2006"
    80  		DefaultLogger.SetLogTimeFormat(format)
    81  		DefaultLogger.SetLogLevel(LogLevelInfo)
    82  		DefaultLogger.Infof("info")
    83  		t, err := time.Parse(format, b.String()[:b.Len()-6])
    84  		Expect(err).ToNot(HaveOccurred())
    85  		Expect(t).To(BeTemporally("~", time.Now(), 25*time.Hour))
    86  	})
    87  
    88  	It("says whether debug is enabled", func() {
    89  		Expect(DefaultLogger.Debug()).To(BeFalse())
    90  		DefaultLogger.SetLogLevel(LogLevelDebug)
    91  		Expect(DefaultLogger.Debug()).To(BeTrue())
    92  	})
    93  
    94  	It("adds a prefix", func() {
    95  		DefaultLogger.SetLogLevel(LogLevelDebug)
    96  		prefixLogger := DefaultLogger.WithPrefix("prefix")
    97  		prefixLogger.Debugf("debug")
    98  		Expect(b.String()).To(ContainSubstring("prefix"))
    99  		Expect(b.String()).To(ContainSubstring("debug"))
   100  	})
   101  
   102  	It("adds multiple prefixes", func() {
   103  		DefaultLogger.SetLogLevel(LogLevelDebug)
   104  		prefixLogger := DefaultLogger.WithPrefix("prefix1")
   105  		prefixPrefixLogger := prefixLogger.WithPrefix("prefix2")
   106  		prefixPrefixLogger.Debugf("debug")
   107  		Expect(b.String()).To(ContainSubstring("prefix"))
   108  		Expect(b.String()).To(ContainSubstring("debug"))
   109  	})
   110  
   111  	Context("reading from env", func() {
   112  		BeforeEach(func() {
   113  			Expect(DefaultLogger.(*defaultLogger).logLevel).To(Equal(LogLevelNothing))
   114  		})
   115  
   116  		It("reads DEBUG", func() {
   117  			os.Setenv(logEnv, "DEBUG")
   118  			Expect(readLoggingEnv()).To(Equal(LogLevelDebug))
   119  		})
   120  
   121  		It("reads debug", func() {
   122  			os.Setenv(logEnv, "debug")
   123  			Expect(readLoggingEnv()).To(Equal(LogLevelDebug))
   124  		})
   125  
   126  		It("reads INFO", func() {
   127  			os.Setenv(logEnv, "INFO")
   128  			readLoggingEnv()
   129  			Expect(readLoggingEnv()).To(Equal(LogLevelInfo))
   130  		})
   131  
   132  		It("reads ERROR", func() {
   133  			os.Setenv(logEnv, "ERROR")
   134  			Expect(readLoggingEnv()).To(Equal(LogLevelError))
   135  		})
   136  
   137  		It("does not error reading invalid log levels from env", func() {
   138  			os.Setenv(logEnv, "")
   139  			Expect(readLoggingEnv()).To(Equal(LogLevelNothing))
   140  			os.Setenv(logEnv, "asdf")
   141  			Expect(readLoggingEnv()).To(Equal(LogLevelNothing))
   142  		})
   143  	})
   144  })