forked from quic-go/quic-go
use the standard library log package for logging
This commit is contained in:
38
utils/log.go
38
utils/log.go
@@ -1,11 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -26,18 +24,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
logLevel = LogLevelNothing
|
||||
out io.Writer = os.Stdout
|
||||
timeFormat = "15:04:05.000"
|
||||
|
||||
mutex sync.Mutex
|
||||
logLevel = LogLevelNothing
|
||||
timeFormat = ""
|
||||
)
|
||||
|
||||
// SetLogWriter sets the log writer.
|
||||
func SetLogWriter(w io.Writer) {
|
||||
out = w
|
||||
}
|
||||
|
||||
// SetLogLevel sets the log level
|
||||
func SetLogLevel(level LogLevel) {
|
||||
logLevel = level
|
||||
@@ -46,42 +36,36 @@ func SetLogLevel(level LogLevel) {
|
||||
// SetLogTimeFormat sets the format of the timestamp
|
||||
// an empty string disables the logging of timestamps
|
||||
func SetLogTimeFormat(format string) {
|
||||
log.SetFlags(0) // disable timestamp logging done by the log package
|
||||
timeFormat = format
|
||||
}
|
||||
|
||||
// Debugf logs something
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
if logLevel == LogLevelDebug {
|
||||
mutex.Lock()
|
||||
logTimestamp()
|
||||
fmt.Fprintf(out, format+"\n", args...)
|
||||
mutex.Unlock()
|
||||
logMessage(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Infof logs something
|
||||
func Infof(format string, args ...interface{}) {
|
||||
if logLevel <= LogLevelInfo {
|
||||
mutex.Lock()
|
||||
logTimestamp()
|
||||
fmt.Fprintf(out, format+"\n", args...)
|
||||
mutex.Unlock()
|
||||
logMessage(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// Errorf logs something
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
if logLevel <= LogLevelError {
|
||||
mutex.Lock()
|
||||
logTimestamp()
|
||||
fmt.Fprintf(out, format+"\n", args...)
|
||||
mutex.Unlock()
|
||||
logMessage(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func logTimestamp() {
|
||||
func logMessage(format string, args ...interface{}) {
|
||||
if len(timeFormat) > 0 {
|
||||
fmt.Fprintf(out, time.Now().Format(timeFormat)+" ")
|
||||
log.Printf(time.Now().Format(timeFormat)+" "+format, args...)
|
||||
} else {
|
||||
log.Printf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -12,16 +13,20 @@ import (
|
||||
var _ = Describe("Log", func() {
|
||||
var (
|
||||
b *bytes.Buffer
|
||||
|
||||
initialTimeFormat string
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
b = bytes.NewBuffer([]byte{})
|
||||
out = b
|
||||
log.SetOutput(b)
|
||||
initialTimeFormat = timeFormat
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
out = os.Stdout
|
||||
log.SetOutput(os.Stdout)
|
||||
SetLogLevel(LogLevelNothing)
|
||||
timeFormat = initialTimeFormat
|
||||
})
|
||||
|
||||
It("log level nothing", func() {
|
||||
|
||||
Reference in New Issue
Block a user