Commit 3f6dfba1 authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

Redo the plugin log PR (#2315)

* Redo the plugin log PR

Remove the code duplication and call of the "official" functions. This
is the second(?) time we forgot to update the other half, so remove that
problem entirely.

Also add a test if the correct (within limits) time in front of the log
line.
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>

* Remove pFormat
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent 669b99d9
......@@ -2,7 +2,6 @@ package log
import (
"fmt"
golog "log"
"os"
)
......@@ -13,16 +12,14 @@ type P struct {
// NewWithPlugin returns a logger that includes "plugin/name: " in the log message.
// I.e [INFO] plugin/<name>: message.
func NewWithPlugin(name string) P { return P{name} }
func NewWithPlugin(name string) P { return P{"plugin/" + name + ": "} }
func (p P) logf(level, format string, v ...interface{}) {
s := level + pFormat(p.plugin) + fmt.Sprintf(format, v...)
golog.Print(s)
log(level, p.plugin, fmt.Sprintf(format, v...))
}
func (p P) log(level string, v ...interface{}) {
s := level + pFormat(p.plugin) + fmt.Sprint(v...)
golog.Print(s)
log(level+p.plugin, v...)
}
// Debug logs as log.Debug.
......@@ -64,5 +61,3 @@ func (p P) Fatal(v ...interface{}) { p.log(fatal, v...); os.Exit(1) }
// Fatalf logs as log.Fatalf and calls os.Exit(1).
func (p P) Fatalf(format string, v ...interface{}) { p.logf(fatal, format, v...); os.Exit(1) }
func pFormat(s string) string { return "plugin/" + s + ": " }
......@@ -19,3 +19,19 @@ func TestPlugins(t *testing.T) {
t.Errorf("Expected log to be %s, got %s", info+ts, x)
}
}
func TestPluginsDateTime(t *testing.T) {
var f bytes.Buffer
const ts = "test"
golog.SetFlags(0) // Set to 0 because we're doing our own time, with timezone
golog.SetOutput(&f)
lg := NewWithPlugin("testplugin")
lg.Info(ts)
// rude check if the date/time is there
str := f.String()
if str[4] != '-' || str[7] != '-' || str[10] != 'T' {
t.Errorf("Expected date got %s...", str[:15])
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment