forked from quic-go/quic-go
make the log level env variable a string
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"
|
||||
|
||||
const (
|
||||
// LogLevelNothing disables
|
||||
LogLevelNothing LogLevel = 0
|
||||
LogLevelNothing LogLevel = iota
|
||||
// LogLevelError enables err logs
|
||||
LogLevelError LogLevel = 1
|
||||
LogLevelError
|
||||
// LogLevelInfo enables info logs (e.g. packets)
|
||||
LogLevelInfo LogLevel = 2
|
||||
LogLevelInfo
|
||||
// LogLevelDebug enables debug logs (e.g. packet contents)
|
||||
LogLevelDebug LogLevel = 3
|
||||
LogLevelDebug
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -97,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, "3")
|
||||
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