forked from quic-go/quic-go
Read the initial log level from the QUIC_GO_LOG_LEVEL env if present
We deliberately ignore any parsing errors. Fixes #501.
This commit is contained in:
28
utils/log.go
28
utils/log.go
@@ -4,15 +4,16 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var out io.Writer = os.Stdout
|
||||
|
||||
// LogLevel of quic-go
|
||||
type LogLevel uint8
|
||||
|
||||
const (
|
||||
logEnv = "QUIC_GO_LOG_LEVEL"
|
||||
|
||||
// LogLevelDebug enables debug logs (e.g. packet contents)
|
||||
LogLevelDebug LogLevel = iota
|
||||
// LogLevelInfo enables info logs (e.g. packets)
|
||||
@@ -23,9 +24,12 @@ const (
|
||||
LogLevelNothing
|
||||
)
|
||||
|
||||
var logLevel = LogLevelNothing
|
||||
var (
|
||||
logLevel = LogLevelNothing
|
||||
out io.Writer = os.Stdout
|
||||
|
||||
var mutex sync.Mutex
|
||||
mutex sync.Mutex
|
||||
)
|
||||
|
||||
// SetLogWriter sets the log writer.
|
||||
func SetLogWriter(w io.Writer) {
|
||||
@@ -68,3 +72,19 @@ func Errorf(format string, args ...interface{}) {
|
||||
func Debug() bool {
|
||||
return logLevel == LogLevelDebug
|
||||
}
|
||||
|
||||
func init() {
|
||||
readLoggingEnv()
|
||||
}
|
||||
|
||||
func readLoggingEnv() {
|
||||
env := os.Getenv(logEnv)
|
||||
if env == "" {
|
||||
return
|
||||
}
|
||||
level, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
logLevel = LogLevel(level)
|
||||
}
|
||||
|
||||
@@ -60,4 +60,21 @@ var _ = Describe("Log", func() {
|
||||
SetLogLevel(LogLevelDebug)
|
||||
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))
|
||||
})
|
||||
|
||||
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