logging: unify pkg/log and plugin/log (#2245)

Default to using pkg/log for all logging and use a fixed time prefix
which is RFC3339Millli (doesn't exist in time, so we just extended
RFC3339), i.e. Nano might be pushing it.

Logs go from:

2018/10/30 19:14:55 [INFO] CoreDNS-1.2.5
2018/10/30 19:14:55 [INFO] linux/amd64, go1.11,

to:

2018-10-30T19:10:07.547Z [INFO] CoreDNS-1.2.5
2018-10-30T19:10:07.547Z [INFO] linux/amd64, go1.11,

Which includes the timezone - which oddly the std log package doesn't
natively do.

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2018-10-31 21:32:23 +00:00
committed by GitHub
parent cfbfa5c00e
commit 2456416444
7 changed files with 45 additions and 49 deletions

View File

@@ -1,11 +1,11 @@
// Package log implements a small wrapper around the std lib log package.
// It implements log levels by prefixing the logs with [INFO], [DEBUG],
// [WARNING] or [ERROR].
// It implements log levels by prefixing the logs with the current time
// with in RFC3339Milli and [INFO], [DEBUG], [WARNING] or [ERROR].
// Debug logging is available and enabled if the *debug* plugin is used.
//
// log.Info("this is some logging"), will log on the Info level.
//
// log.Debug("this is debug output"), will log in the Debug level.
// log.Debug("this is debug output"), will log in the Debug level, etc.
package log
import (
@@ -13,19 +13,24 @@ import (
"io/ioutil"
golog "log"
"os"
"time"
)
// D controls whether we should output debug logs. If true, we do.
var D bool
// RFC3339Milli doesn't exist, invent it here.
func clock() string { return time.Now().Format("2006-01-02T15:04:05.999Z07:00") }
// logf calls log.Printf prefixed with level.
func logf(level, format string, v ...interface{}) {
s := level + fmt.Sprintf(format, v...)
golog.Print(s)
golog.Print(clock(), level, fmt.Sprintf(format, v...))
}
// log calls log.Print prefixed with level.
func log(level string, v ...interface{}) { s := level + fmt.Sprint(v...); golog.Print(s) }
func log(level string, v ...interface{}) {
golog.Print(clock(), level, fmt.Sprint(v...))
}
// Debug is equivalent to log.Print(), but prefixed with "[DEBUG] ". It only outputs something
// if D is true.
@@ -75,9 +80,9 @@ func Fatalf(format string, v ...interface{}) { logf(fatal, format, v...); os.Exi
func Discard() { golog.SetOutput(ioutil.Discard) }
const (
debug = "[DEBUG] "
err = "[ERROR] "
fatal = "[FATAL] "
info = "[INFO] "
warning = "[WARNING] "
debug = " [DEBUG] "
err = " [ERROR] "
fatal = " [FATAL] "
info = " [INFO] "
warning = " [WARNING] "
)