forked from quic-go/quic-go
implement a prefix logger
This commit is contained in:
@@ -28,6 +28,7 @@ const logEnv = "QUIC_GO_LOG_LEVEL"
|
||||
type Logger interface {
|
||||
SetLogLevel(LogLevel)
|
||||
SetLogTimeFormat(format string)
|
||||
WithPrefix(prefix string) Logger
|
||||
Debug() bool
|
||||
|
||||
Errorf(format string, args ...interface{})
|
||||
@@ -39,6 +40,8 @@ type Logger interface {
|
||||
var DefaultLogger Logger
|
||||
|
||||
type defaultLogger struct {
|
||||
prefix string
|
||||
|
||||
logLevel LogLevel
|
||||
timeFormat string
|
||||
}
|
||||
@@ -79,10 +82,25 @@ func (l *defaultLogger) Errorf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
func (l *defaultLogger) logMessage(format string, args ...interface{}) {
|
||||
var pre string
|
||||
|
||||
if len(l.timeFormat) > 0 {
|
||||
log.Printf(time.Now().Format(l.timeFormat)+" "+format, args...)
|
||||
} else {
|
||||
log.Printf(format, args...)
|
||||
pre = time.Now().Format(l.timeFormat) + " "
|
||||
}
|
||||
if len(l.prefix) > 0 {
|
||||
pre += l.prefix + " "
|
||||
}
|
||||
log.Printf(pre+format, args...)
|
||||
}
|
||||
|
||||
func (l *defaultLogger) WithPrefix(prefix string) Logger {
|
||||
if len(l.prefix) > 0 {
|
||||
prefix = l.prefix + " " + prefix
|
||||
}
|
||||
return &defaultLogger{
|
||||
logLevel: l.logLevel,
|
||||
timeFormat: l.timeFormat,
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ var _ = Describe("Log", func() {
|
||||
DefaultLogger.Debugf("debug")
|
||||
DefaultLogger.Infof("info")
|
||||
DefaultLogger.Errorf("err")
|
||||
Expect(b.Bytes()).To(Equal([]byte("")))
|
||||
Expect(b.String()).To(BeEmpty())
|
||||
})
|
||||
|
||||
It("log level err", func() {
|
||||
@@ -43,9 +43,9 @@ var _ = Describe("Log", func() {
|
||||
DefaultLogger.Debugf("debug")
|
||||
DefaultLogger.Infof("info")
|
||||
DefaultLogger.Errorf("err")
|
||||
Expect(b.Bytes()).To(ContainSubstring("err\n"))
|
||||
Expect(b.Bytes()).ToNot(ContainSubstring("info"))
|
||||
Expect(b.Bytes()).ToNot(ContainSubstring("debug"))
|
||||
Expect(b.String()).To(ContainSubstring("err\n"))
|
||||
Expect(b.String()).ToNot(ContainSubstring("info"))
|
||||
Expect(b.String()).ToNot(ContainSubstring("debug"))
|
||||
})
|
||||
|
||||
It("log level info", func() {
|
||||
@@ -53,9 +53,9 @@ var _ = Describe("Log", func() {
|
||||
DefaultLogger.Debugf("debug")
|
||||
DefaultLogger.Infof("info")
|
||||
DefaultLogger.Errorf("err")
|
||||
Expect(b.Bytes()).To(ContainSubstring("err\n"))
|
||||
Expect(b.Bytes()).To(ContainSubstring("info\n"))
|
||||
Expect(b.Bytes()).ToNot(ContainSubstring("debug"))
|
||||
Expect(b.String()).To(ContainSubstring("err\n"))
|
||||
Expect(b.String()).To(ContainSubstring("info\n"))
|
||||
Expect(b.String()).ToNot(ContainSubstring("debug"))
|
||||
})
|
||||
|
||||
It("log level debug", func() {
|
||||
@@ -63,16 +63,16 @@ var _ = Describe("Log", func() {
|
||||
DefaultLogger.Debugf("debug")
|
||||
DefaultLogger.Infof("info")
|
||||
DefaultLogger.Errorf("err")
|
||||
Expect(b.Bytes()).To(ContainSubstring("err\n"))
|
||||
Expect(b.Bytes()).To(ContainSubstring("info\n"))
|
||||
Expect(b.Bytes()).To(ContainSubstring("debug\n"))
|
||||
Expect(b.String()).To(ContainSubstring("err\n"))
|
||||
Expect(b.String()).To(ContainSubstring("info\n"))
|
||||
Expect(b.String()).To(ContainSubstring("debug\n"))
|
||||
})
|
||||
|
||||
It("doesn't add a timestamp if the time format is empty", func() {
|
||||
DefaultLogger.SetLogLevel(LogLevelDebug)
|
||||
DefaultLogger.SetLogTimeFormat("")
|
||||
DefaultLogger.Debugf("debug")
|
||||
Expect(b.Bytes()).To(Equal([]byte("debug\n")))
|
||||
Expect(b.String()).To(Equal("debug\n"))
|
||||
})
|
||||
|
||||
It("adds a timestamp", func() {
|
||||
@@ -80,7 +80,7 @@ var _ = Describe("Log", func() {
|
||||
DefaultLogger.SetLogTimeFormat(format)
|
||||
DefaultLogger.SetLogLevel(LogLevelInfo)
|
||||
DefaultLogger.Infof("info")
|
||||
t, err := time.Parse(format, string(b.Bytes()[:b.Len()-6]))
|
||||
t, err := time.Parse(format, string(b.String()[:b.Len()-6]))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(t).To(BeTemporally("~", time.Now(), 25*time.Hour))
|
||||
})
|
||||
@@ -91,6 +91,23 @@ var _ = Describe("Log", func() {
|
||||
Expect(DefaultLogger.Debug()).To(BeTrue())
|
||||
})
|
||||
|
||||
It("adds a prefix", func() {
|
||||
DefaultLogger.SetLogLevel(LogLevelDebug)
|
||||
prefixLogger := DefaultLogger.WithPrefix("prefix")
|
||||
prefixLogger.Debugf("debug")
|
||||
Expect(b.String()).To(ContainSubstring("prefix"))
|
||||
Expect(b.String()).To(ContainSubstring("debug"))
|
||||
})
|
||||
|
||||
It("adds multiple prefixes", func() {
|
||||
DefaultLogger.SetLogLevel(LogLevelDebug)
|
||||
prefixLogger := DefaultLogger.WithPrefix("prefix1")
|
||||
prefixPrefixLogger := prefixLogger.WithPrefix("prefix2")
|
||||
prefixPrefixLogger.Debugf("debug")
|
||||
Expect(b.String()).To(ContainSubstring("prefix"))
|
||||
Expect(b.String()).To(ContainSubstring("debug"))
|
||||
})
|
||||
|
||||
Context("reading from env", func() {
|
||||
BeforeEach(func() {
|
||||
Expect(DefaultLogger.(*defaultLogger).logLevel).To(Equal(LogLevelNothing))
|
||||
|
||||
Reference in New Issue
Block a user