-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixlog.go
151 lines (130 loc) · 4.28 KB
/
prefixlog.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package eventlog
import (
"strings"
"github.com/ipfs/go-ipfs/util"
)
// StandardLogger provides API compatibility with standard printf loggers
// eg. go-logging
type StandardLogger interface {
Critical(args ...interface{})
Criticalf(format string, args ...interface{})
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Notice(args ...interface{})
Noticef(format string, args ...interface{})
Panic(args ...interface{})
Panicf(format string, args ...interface{})
Warning(args ...interface{})
Warningf(format string, args ...interface{})
}
// StandardLogger provides API compatibility with standard printf loggers
// eg. go-logging
type PrefixLogger interface {
StandardLogger
Format() string
Args() []interface{}
Prefix(fmt string, args ...interface{}) PrefixLogger
}
// Logger retrieves an event logger by name
func Logger(system string) PrefixLogger {
// TODO if we would like to adjust log levels at run-time. Store this event
// logger in a map (just like the util.Logger impl)
logger := util.Logger(system)
return Prefix(logger, "")
}
func Prefix(l StandardLogger, format string, args ...interface{}) PrefixLogger {
return &prefixLogger{logger: l, format: format, args: args}
}
type prefixLogger struct {
logger StandardLogger
format string
args []interface{}
}
func (pl *prefixLogger) Format() string {
return pl.format
}
func (pl *prefixLogger) Args() []interface{} {
return pl.args
}
func (pl *prefixLogger) Prefix(fmt string, args ...interface{}) PrefixLogger {
return Prefix(pl, fmt, args...)
}
func (pl *prefixLogger) prepend(fmt string, args []interface{}) (string, []interface{}) {
together := make([]interface{}, 0, len(pl.args)+len(args))
together = append(together, pl.args...)
together = append(together, args...)
if len(pl.format) > 0 {
fmt = pl.format + " " + fmt
}
return fmt, together
}
func valfmtn(count int) string {
s := strings.Repeat("%v ", count)
s = s[:len(s)-1] // remove last space
return s
}
type logFunc func(args ...interface{})
type logFuncf func(fmt string, args ...interface{})
func (pl *prefixLogger) logFunc(f logFuncf, args ...interface{}) {
// need to actually use the format version, with extra fmt strings appended
fmt := valfmtn(len(args))
pl.logFuncf(f, fmt, args...)
}
func (pl *prefixLogger) logFuncf(f logFuncf, format string, args ...interface{}) {
format, args = pl.prepend(format, args)
f(format, args...)
}
func (pl *prefixLogger) Critical(args ...interface{}) {
pl.logFunc(pl.logger.Criticalf, args...)
}
func (pl *prefixLogger) Debug(args ...interface{}) {
pl.logFunc(pl.logger.Debugf, args...)
}
func (pl *prefixLogger) Error(args ...interface{}) {
pl.logFunc(pl.logger.Errorf, args...)
}
func (pl *prefixLogger) Fatal(args ...interface{}) {
pl.logFunc(pl.logger.Fatalf, args...)
}
func (pl *prefixLogger) Info(args ...interface{}) {
pl.logFunc(pl.logger.Infof, args...)
}
func (pl *prefixLogger) Notice(args ...interface{}) {
pl.logFunc(pl.logger.Noticef, args...)
}
func (pl *prefixLogger) Panic(args ...interface{}) {
pl.logFunc(pl.logger.Panicf, args...)
}
func (pl *prefixLogger) Warning(args ...interface{}) {
pl.logFunc(pl.logger.Warningf, args...)
}
func (pl *prefixLogger) Criticalf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Criticalf, format, args...)
}
func (pl *prefixLogger) Debugf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Debugf, format, args...)
}
func (pl *prefixLogger) Errorf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Errorf, format, args...)
}
func (pl *prefixLogger) Fatalf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Fatalf, format, args...)
}
func (pl *prefixLogger) Infof(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Infof, format, args...)
}
func (pl *prefixLogger) Noticef(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Noticef, format, args...)
}
func (pl *prefixLogger) Panicf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Panicf, format, args...)
}
func (pl *prefixLogger) Warningf(format string, args ...interface{}) {
pl.logFuncf(pl.logger.Warningf, format, args...)
}