prefix logger
this commit adds a logger with prefixes
Juan Batiz-Benet committed
Jan 3, 2015 at 04:06 UTC
623625845dd7ead2c9ad6bafcc7fb93d9bf772b8
2 files changed
+167
-27
util/eventlog/log.go
+16
-27
@@ -2,14 +2,19 @@ package eventlog
2
3
import (
4
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
- logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
6
- "github.com/jbenet/go-ipfs/util"
5
+
6
+ prelog "github.com/jbenet/go-ipfs/util/prefixlog"
7
)
8
9
// EventLogger extends the StandardLogger interface to allow for log items
10
// containing structured metadata
11
type EventLogger interface {
12
- StandardLogger
12
+ prelog.StandardLogger
13
+
14
+ // Prefix is like PrefixLogger.Prefix. We override it here
15
+ // because the type changes (we return EventLogger).
16
+ // It's what happens when you wrap interfaces.
17
+ Prefix(fmt string, args ...interface{}) EventLogger
18
19
// Event merges structured data from the provided inputs into a single
20
// machine-readable log event.
@@ -27,43 +32,27 @@ type EventLogger interface {
32
Event(ctx context.Context, event string, m ...Loggable)
33
}
34
30
-// StandardLogger provides API compatibility with standard printf loggers
31
-// eg. go-logging
32
-type StandardLogger interface {
33
- Critical(args ...interface{})
34
- Criticalf(format string, args ...interface{})
35
- Debug(args ...interface{})
36
- Debugf(format string, args ...interface{})
37
- Error(args ...interface{})
38
- Errorf(format string, args ...interface{})
39
- Fatal(args ...interface{})
40
- Fatalf(format string, args ...interface{})
41
- Info(args ...interface{})
42
- Infof(format string, args ...interface{})
43
- Notice(args ...interface{})
44
- Noticef(format string, args ...interface{})
45
- Panic(args ...interface{})
46
- Panicf(format string, args ...interface{})
47
- Warning(args ...interface{})
48
- Warningf(format string, args ...interface{})
49
-}
50
-
35
// Logger retrieves an event logger by name
36
func Logger(system string) EventLogger {
37
38
// TODO if we would like to adjust log levels at run-time. Store this event
39
// logger in a map (just like the util.Logger impl)
56
-
57
- return &eventLogger{system: system, Logger: util.Logger(system)}
40
+ return &eventLogger{system: system, PrefixLogger: prelog.Logger(system)}
41
}
42
43
// eventLogger implements the EventLogger and wraps a go-logging Logger
44
type eventLogger struct {
62
- *logging.Logger
45
+ prelog.PrefixLogger
46
+
47
system string
48
// TODO add log-level
49
}
50
51
+func (el *eventLogger) Prefix(fmt string, args ...interface{}) EventLogger {
52
+ l := el.PrefixLogger.Prefix(fmt, args...)
53
+ return &eventLogger{system: el.system, PrefixLogger: l}
54
+}
55
+
56
func (el *eventLogger) Event(ctx context.Context, event string, metadata ...Loggable) {
57
58
// Collect loggables for later logging
util/prefixlog/prefixlog.go
new
+151
@@ -0,0 +1,151 @@
1
+package eventlog
2
+
3
+import (
4
+ "strings"
5
+
6
+ "github.com/jbenet/go-ipfs/util"
7
+)
8
+
9
+// StandardLogger provides API compatibility with standard printf loggers
10
+// eg. go-logging
11
+type StandardLogger interface {
12
+ Critical(args ...interface{})
13
+ Criticalf(format string, args ...interface{})
14
+ Debug(args ...interface{})
15
+ Debugf(format string, args ...interface{})
16
+ Error(args ...interface{})
17
+ Errorf(format string, args ...interface{})
18
+ Fatal(args ...interface{})
19
+ Fatalf(format string, args ...interface{})
20
+ Info(args ...interface{})
21
+ Infof(format string, args ...interface{})
22
+ Notice(args ...interface{})
23
+ Noticef(format string, args ...interface{})
24
+ Panic(args ...interface{})
25
+ Panicf(format string, args ...interface{})
26
+ Warning(args ...interface{})
27
+ Warningf(format string, args ...interface{})
28
+}
29
+
30
+// StandardLogger provides API compatibility with standard printf loggers
31
+// eg. go-logging
32
+type PrefixLogger interface {
33
+ StandardLogger
34
+
35
+ Format() string
36
+ Args() []interface{}
37
+
38
+ Prefix(fmt string, args ...interface{}) PrefixLogger
39
+}
40
+
41
+// Logger retrieves an event logger by name
42
+func Logger(system string) PrefixLogger {
43
+
44
+ // TODO if we would like to adjust log levels at run-time. Store this event
45
+ // logger in a map (just like the util.Logger impl)
46
+
47
+ logger := util.Logger(system)
48
+ return Prefix(logger, "")
49
+}
50
+
51
+func Prefix(l StandardLogger, format string, args ...interface{}) PrefixLogger {
52
+ return &prefixLogger{logger: l, format: format, args: args}
53
+}
54
+
55
+type prefixLogger struct {
56
+ logger StandardLogger
57
+ format string
58
+ args []interface{}
59
+}
60
+
61
+func (pl *prefixLogger) Format() string {
62
+ return pl.format
63
+}
64
+
65
+func (pl *prefixLogger) Args() []interface{} {
66
+ return pl.args
67
+}
68
+
69
+func (pl *prefixLogger) Prefix(fmt string, args ...interface{}) PrefixLogger {
70
+ return Prefix(pl, fmt, args...)
71
+}
72
+
73
+func (pl *prefixLogger) prepend(fmt string, args []interface{}) (string, []interface{}) {
74
+ together := make([]interface{}, 0, len(pl.args)+len(args))
75
+ together = append(together, pl.args...)
76
+ together = append(together, args...)
77
+ if len(pl.format) > 0 {
78
+ fmt = pl.format + " " + fmt
79
+ }
80
+ return fmt, together
81
+}
82
+
83
+func valfmtn(count int) string {
84
+ s := strings.Repeat("%v ", count)
85
+ s = s[:len(s)-1] // remove last space
86
+ return s
87
+}
88
+
89
+type logFunc func(args ...interface{})
90
+type logFuncf func(fmt string, args ...interface{})
91
+
92
+func (pl *prefixLogger) logFunc(f logFuncf, args ...interface{}) {
93
+ // need to actually use the format version, with extra fmt strings appended
94
+ fmt := valfmtn(len(args))
95
+ pl.logFuncf(f, fmt, args...)
96
+}
97
+
98
+func (pl *prefixLogger) logFuncf(f logFuncf, format string, args ...interface{}) {
99
+ format, args = pl.prepend(format, args)
100
+ f(format, args...)
101
+}
102
+
103
+func (pl *prefixLogger) Critical(args ...interface{}) {
104
+ pl.logFunc(pl.logger.Criticalf, args...)
105
+}
106
+func (pl *prefixLogger) Debug(args ...interface{}) {
107
+ pl.logFunc(pl.logger.Debugf, args...)
108
+}
109
+func (pl *prefixLogger) Error(args ...interface{}) {
110
+ pl.logFunc(pl.logger.Errorf, args...)
111
+}
112
+func (pl *prefixLogger) Fatal(args ...interface{}) {
113
+ pl.logFunc(pl.logger.Fatalf, args...)
114
+}
115
+func (pl *prefixLogger) Info(args ...interface{}) {
116
+ pl.logFunc(pl.logger.Infof, args...)
117
+}
118
+func (pl *prefixLogger) Notice(args ...interface{}) {
119
+ pl.logFunc(pl.logger.Noticef, args...)
120
+}
121
+func (pl *prefixLogger) Panic(args ...interface{}) {
122
+ pl.logFunc(pl.logger.Panicf, args...)
123
+}
124
+func (pl *prefixLogger) Warning(args ...interface{}) {
125
+ pl.logFunc(pl.logger.Warningf, args...)
126
+}
127
+
128
+func (pl *prefixLogger) Criticalf(format string, args ...interface{}) {
129
+ pl.logFuncf(pl.logger.Criticalf, format, args...)
130
+}
131
+func (pl *prefixLogger) Debugf(format string, args ...interface{}) {
132
+ pl.logFuncf(pl.logger.Debugf, format, args...)
133
+}
134
+func (pl *prefixLogger) Errorf(format string, args ...interface{}) {
135
+ pl.logFuncf(pl.logger.Errorf, format, args...)
136
+}
137
+func (pl *prefixLogger) Fatalf(format string, args ...interface{}) {
138
+ pl.logFuncf(pl.logger.Fatalf, format, args...)
139
+}
140
+func (pl *prefixLogger) Infof(format string, args ...interface{}) {
141
+ pl.logFuncf(pl.logger.Infof, format, args...)
142
+}
143
+func (pl *prefixLogger) Noticef(format string, args ...interface{}) {
144
+ pl.logFuncf(pl.logger.Noticef, format, args...)
145
+}
146
+func (pl *prefixLogger) Panicf(format string, args ...interface{}) {
147
+ pl.logFuncf(pl.logger.Panicf, format, args...)
148
+}
149
+func (pl *prefixLogger) Warningf(format string, args ...interface{}) {
150
+ pl.logFuncf(pl.logger.Warningf, format, args...)
151
+}