forked from quic-go/quic-go
Merge pull request #647 from lucas-clemente/log-level-numbers
better log levels
This commit is contained in:
@@ -6,4 +6,5 @@
|
||||
- Add a `quic.Config` option to request truncation of the connection ID from a server
|
||||
- Add a `quic.Config` option to configure the source address validation
|
||||
- Add a `quic.Config` option to configure the handshake timeout
|
||||
- Changed the log level environment variable to only accept strings ("DEBUG", "INFO", "ERROR"), see [the wiki](https://github.com/lucas-clemente/quic-go/wiki/Logging) for more details.
|
||||
- Various bugfixes
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LogLevel of quic-go
|
||||
type LogLevel uint8
|
||||
|
||||
const (
|
||||
logEnv = "QUIC_GO_LOG_LEVEL"
|
||||
const logEnv = "QUIC_GO_LOG_LEVEL"
|
||||
|
||||
// LogLevelDebug enables debug logs (e.g. packet contents)
|
||||
LogLevelDebug LogLevel = iota
|
||||
// LogLevelInfo enables info logs (e.g. packets)
|
||||
LogLevelInfo
|
||||
const (
|
||||
// LogLevelNothing disables
|
||||
LogLevelNothing LogLevel = iota
|
||||
// LogLevelError enables err logs
|
||||
LogLevelError
|
||||
// LogLevelNothing disables
|
||||
LogLevelNothing
|
||||
// LogLevelInfo enables info logs (e.g. packets)
|
||||
LogLevelInfo
|
||||
// LogLevelDebug enables debug logs (e.g. packet contents)
|
||||
LogLevelDebug
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -49,14 +49,14 @@ func Debugf(format string, args ...interface{}) {
|
||||
|
||||
// Infof logs something
|
||||
func Infof(format string, args ...interface{}) {
|
||||
if logLevel <= LogLevelInfo {
|
||||
if logLevel >= LogLevelInfo {
|
||||
logMessage(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Errorf logs something
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
if logLevel <= LogLevelError {
|
||||
if logLevel >= LogLevelError {
|
||||
logMessage(format, args...)
|
||||
}
|
||||
}
|
||||
@@ -79,13 +79,16 @@ func init() {
|
||||
}
|
||||
|
||||
func readLoggingEnv() {
|
||||
env := os.Getenv(logEnv)
|
||||
if env == "" {
|
||||
switch os.Getenv(logEnv) {
|
||||
case "":
|
||||
return
|
||||
case "DEBUG":
|
||||
logLevel = LogLevelDebug
|
||||
case "INFO":
|
||||
logLevel = LogLevelInfo
|
||||
case "ERROR":
|
||||
logLevel = LogLevelError
|
||||
default:
|
||||
fmt.Fprintln(os.Stderr, "invalid quic-go log level, see https://github.com/lucas-clemente/quic-go/wiki/Logging")
|
||||
}
|
||||
level, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
logLevel = LogLevel(level)
|
||||
}
|
||||
|
||||
@@ -29,6 +29,13 @@ var _ = Describe("Log", func() {
|
||||
timeFormat = initialTimeFormat
|
||||
})
|
||||
|
||||
It("the log level has the correct numeric value", func() {
|
||||
Expect(LogLevelNothing).To(BeEquivalentTo(0))
|
||||
Expect(LogLevelError).To(BeEquivalentTo(1))
|
||||
Expect(LogLevelInfo).To(BeEquivalentTo(2))
|
||||
Expect(LogLevelDebug).To(BeEquivalentTo(3))
|
||||
})
|
||||
|
||||
It("log level nothing", func() {
|
||||
SetLogLevel(LogLevelNothing)
|
||||
Debugf("debug")
|
||||
@@ -90,20 +97,37 @@ var _ = Describe("Log", func() {
|
||||
Expect(Debug()).To(BeTrue())
|
||||
})
|
||||
|
||||
It("reads log level from env", func() {
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
os.Setenv(logEnv, "1")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelDebug))
|
||||
})
|
||||
Context("reading from env", func() {
|
||||
BeforeEach(func() {
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
})
|
||||
|
||||
It("does not error reading invalid log levels from env", func() {
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
os.Setenv(logEnv, "")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
os.Setenv(logEnv, "asdf")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
It("reads DEBUG", func() {
|
||||
os.Setenv(logEnv, "DEBUG")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelDebug))
|
||||
})
|
||||
|
||||
It("reads INFO", func() {
|
||||
os.Setenv(logEnv, "INFO")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelInfo))
|
||||
})
|
||||
|
||||
It("reads ERROR", func() {
|
||||
os.Setenv(logEnv, "ERROR")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelError))
|
||||
})
|
||||
|
||||
It("does not error reading invalid log levels from env", func() {
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
os.Setenv(logEnv, "")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
os.Setenv(logEnv, "asdf")
|
||||
readLoggingEnv()
|
||||
Expect(logLevel).To(Equal(LogLevelNothing))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user