use the standard library log package for logging

This commit is contained in:
Marten Seemann
2017-05-18 14:29:59 +08:00
parent 7e256e0ebe
commit 0dbbb8d1b3
4 changed files with 26 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package frames
import ( import (
"bytes" "bytes"
"log"
"os" "os"
"time" "time"
@@ -20,12 +21,12 @@ var _ = Describe("Frame logging", func() {
BeforeEach(func() { BeforeEach(func() {
buf.Reset() buf.Reset()
utils.SetLogLevel(utils.LogLevelDebug) utils.SetLogLevel(utils.LogLevelDebug)
utils.SetLogWriter(&buf) log.SetOutput(&buf)
}) })
AfterSuite(func() { AfterSuite(func() {
utils.SetLogLevel(utils.LogLevelNothing) utils.SetLogLevel(utils.LogLevelNothing)
utils.SetLogWriter(os.Stdout) log.SetOutput(os.Stdout)
}) })
It("doesn't log when debug is disabled", func() { It("doesn't log when debug is disabled", func() {

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"mime/multipart" "mime/multipart"
"net" "net"
"net/http" "net/http"
@@ -78,6 +79,9 @@ func init() {
} }
var _ = BeforeEach(func() { var _ = BeforeEach(func() {
// set custom time format for logs
utils.SetLogTimeFormat("15:04:05.000")
// create a new uploadDir for every test // create a new uploadDir for every test
var err error var err error
uploadDir, err = ioutil.TempDir("", "quic-upload-dest") uploadDir, err = ioutil.TempDir("", "quic-upload-dest")
@@ -95,7 +99,7 @@ var _ = BeforeEach(func() {
if len(logFileName) > 0 { if len(logFileName) > 0 {
logFile, err = os.Create("./log.txt") logFile, err = os.Create("./log.txt")
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
utils.SetLogWriter(logFile) log.SetOutput(logFile)
utils.SetLogLevel(utils.LogLevelDebug) utils.SetLogLevel(utils.LogLevelDebug)
} }
}) })

View File

@@ -1,11 +1,9 @@
package utils package utils
import ( import (
"fmt" "log"
"io"
"os" "os"
"strconv" "strconv"
"sync"
"time" "time"
) )
@@ -27,17 +25,9 @@ const (
var ( var (
logLevel = LogLevelNothing logLevel = LogLevelNothing
out io.Writer = os.Stdout timeFormat = ""
timeFormat = "15:04:05.000"
mutex sync.Mutex
) )
// SetLogWriter sets the log writer.
func SetLogWriter(w io.Writer) {
out = w
}
// SetLogLevel sets the log level // SetLogLevel sets the log level
func SetLogLevel(level LogLevel) { func SetLogLevel(level LogLevel) {
logLevel = level logLevel = level
@@ -46,42 +36,36 @@ func SetLogLevel(level LogLevel) {
// SetLogTimeFormat sets the format of the timestamp // SetLogTimeFormat sets the format of the timestamp
// an empty string disables the logging of timestamps // an empty string disables the logging of timestamps
func SetLogTimeFormat(format string) { func SetLogTimeFormat(format string) {
log.SetFlags(0) // disable timestamp logging done by the log package
timeFormat = format timeFormat = format
} }
// Debugf logs something // Debugf logs something
func Debugf(format string, args ...interface{}) { func Debugf(format string, args ...interface{}) {
if logLevel == LogLevelDebug { if logLevel == LogLevelDebug {
mutex.Lock() logMessage(format, args...)
logTimestamp()
fmt.Fprintf(out, format+"\n", args...)
mutex.Unlock()
} }
} }
// Infof logs something // Infof logs something
func Infof(format string, args ...interface{}) { func Infof(format string, args ...interface{}) {
if logLevel <= LogLevelInfo { if logLevel <= LogLevelInfo {
mutex.Lock() logMessage(format, args...)
logTimestamp()
fmt.Fprintf(out, format+"\n", args...)
mutex.Unlock()
} }
} }
// Errorf logs something // Errorf logs something
func Errorf(format string, args ...interface{}) { func Errorf(format string, args ...interface{}) {
if logLevel <= LogLevelError { if logLevel <= LogLevelError {
mutex.Lock() logMessage(format, args...)
logTimestamp()
fmt.Fprintf(out, format+"\n", args...)
mutex.Unlock()
} }
} }
func logTimestamp() { func logMessage(format string, args ...interface{}) {
if len(timeFormat) > 0 { if len(timeFormat) > 0 {
fmt.Fprintf(out, time.Now().Format(timeFormat)+" ") log.Printf(time.Now().Format(timeFormat)+" "+format, args...)
} else {
log.Printf(format, args...)
} }
} }

View File

@@ -2,6 +2,7 @@ package utils
import ( import (
"bytes" "bytes"
"log"
"os" "os"
"time" "time"
@@ -12,16 +13,20 @@ import (
var _ = Describe("Log", func() { var _ = Describe("Log", func() {
var ( var (
b *bytes.Buffer b *bytes.Buffer
initialTimeFormat string
) )
BeforeEach(func() { BeforeEach(func() {
b = bytes.NewBuffer([]byte{}) b = bytes.NewBuffer([]byte{})
out = b log.SetOutput(b)
initialTimeFormat = timeFormat
}) })
AfterEach(func() { AfterEach(func() {
out = os.Stdout log.SetOutput(os.Stdout)
SetLogLevel(LogLevelNothing) SetLogLevel(LogLevelNothing)
timeFormat = initialTimeFormat
}) })
It("log level nothing", func() { It("log level nothing", func() {