-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_writer.go
81 lines (73 loc) · 1.56 KB
/
log_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package peanut
import (
"fmt"
"log"
"os"
"reflect"
)
// TODO(js) This could have a multi-line mode, where each field is logged to its own line.
var _ Writer = &LogWriter{}
// LogWriter writes records to a log.Logger.
//
// If Logger is nil at runtime, a new log.Logger
// will be created when needed, writing to os.Stderr.
type LogWriter struct {
*base
Logger *log.Logger
Verbose bool
}
func (w *LogWriter) register(x interface{}) (reflect.Type, error) {
if w.base == nil {
w.base = &base{}
if w.Logger == nil {
w.Logger = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}
}
t, ok := w.base.register(x)
if !ok {
return t, nil
}
if err := allFieldsSupportedKinds(x); err != nil {
return nil, err
}
return t, nil
}
// Write is called to persist records.
func (w *LogWriter) Write(x interface{}) error {
t, err := w.register(x)
if err != nil {
return err
}
// Build log message.
n := t.Name()
m := fmt.Sprintf("<%s>", n)
// Concatenate field names and values.
h := w.headersByType[t]
v := stringValues(x)
for i := range v {
m += fmt.Sprintf(" %s:", h[i])
if len(v[i]) > 0 {
m += fmt.Sprintf(" %s", v[i])
}
}
if len(h) == 0 {
m += " -"
}
// Log it.
w.Logger.Println(m)
return nil
}
// Close should be called after successfully writing records.
func (w *LogWriter) Close() error {
if w.Verbose {
w.Logger.Println("Called LogWriter.Close")
}
return nil
}
// Cancel should be called in the event of an error occurring.
func (w *LogWriter) Cancel() error {
if w.Verbose {
w.Logger.Println("Called LogWriter.Cancel")
}
return nil
}