forked from quic-go/quic-go
use the standard library log package for logging
This commit is contained in:
@@ -2,6 +2,7 @@ package frames
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -20,12 +21,12 @@ var _ = Describe("Frame logging", func() {
|
||||
BeforeEach(func() {
|
||||
buf.Reset()
|
||||
utils.SetLogLevel(utils.LogLevelDebug)
|
||||
utils.SetLogWriter(&buf)
|
||||
log.SetOutput(&buf)
|
||||
})
|
||||
|
||||
AfterSuite(func() {
|
||||
utils.SetLogLevel(utils.LogLevelNothing)
|
||||
utils.SetLogWriter(os.Stdout)
|
||||
log.SetOutput(os.Stdout)
|
||||
})
|
||||
|
||||
It("doesn't log when debug is disabled", func() {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -78,6 +79,9 @@ func init() {
|
||||
}
|
||||
|
||||
var _ = BeforeEach(func() {
|
||||
// set custom time format for logs
|
||||
utils.SetLogTimeFormat("15:04:05.000")
|
||||
|
||||
// create a new uploadDir for every test
|
||||
var err error
|
||||
uploadDir, err = ioutil.TempDir("", "quic-upload-dest")
|
||||
@@ -95,7 +99,7 @@ var _ = BeforeEach(func() {
|
||||
if len(logFileName) > 0 {
|
||||
logFile, err = os.Create("./log.txt")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
utils.SetLogWriter(logFile)
|
||||
log.SetOutput(logFile)
|
||||
utils.SetLogLevel(utils.LogLevelDebug)
|
||||
}
|
||||
})
|
||||
|
||||
36
utils/log.go
36
utils/log.go
@@ -1,11 +1,9 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -27,17 +25,9 @@ const (
|
||||
|
||||
var (
|
||||
logLevel = LogLevelNothing
|
||||
out io.Writer = os.Stdout
|
||||
timeFormat = "15:04:05.000"
|
||||
|
||||
mutex sync.Mutex
|
||||
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