-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
188 lines (154 loc) · 4.39 KB
/
log.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2014 Simon Zimmermann. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// pkg log implements a logger. It supports console, file and raven logging.
package log
import (
"errors"
"flag"
"fmt"
"os"
)
var (
// Severity stores the log level
Severity Level
std logger
filename *string
ravenDSN *string
)
func init() {
flag.Var(&Severity, "log", "log level")
filename = flag.String("log-file", "", "If non-empty, write log to this file")
ravenDSN = flag.String("log-raven-dsn", "", "If non-empty, write to raven dsn")
std = new(multiLogger)
}
func ConfigLogger(level string, filename string, ravenDSN string) error {
err := Severity.SetString(level)
if err != nil {
return err
}
ml, ok := std.(*multiLogger)
if !ok {
return errors.New("cannot configure")
}
// TODO
if len(ml.loggers) > 0 {
return errors.New("Cannot configure after log-usage")
}
ml.loggers = append(ml.loggers, &consoleLogger{sev: Severity})
if filename != "" {
ml.loggers = append(ml.loggers, &fileLogger{fname: filename, sev: Severity})
}
if ravenDSN != "" {
ml.loggers = append(ml.loggers, &ravenLogger{dsn: ravenDSN, sev: LevelError})
}
return nil
}
type logger interface {
Output(calldepth int, s string, sev Level) error
}
type multiLogger struct {
loggers []logger
}
func (l multiLogger) Output(calldepth int, s string, sev Level) (err error) {
if len(l.loggers) == 0 {
l.init()
}
for _, w := range l.loggers {
err = w.Output(calldepth, s, sev)
if err != nil {
return
}
}
return
}
func (l *multiLogger) init() {
l.loggers = append(l.loggers, &consoleLogger{sev: Severity})
if *filename != "" {
l.loggers = append(l.loggers, &fileLogger{fname: *filename, sev: Severity})
}
if *ravenDSN != "" {
l.loggers = append(l.loggers, &ravenLogger{dsn: *ravenDSN, sev: LevelError})
}
}
// Debug calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Debug(v ...interface{}) {
if Severity >= LevelDebug {
std.Output(5, fmt.Sprint(v...), LevelDebug)
}
}
// Debugf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, v ...interface{}) {
if Severity >= LevelDebug {
std.Output(5, fmt.Sprintf(format, v...), LevelDebug)
}
}
// Debugln calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Debugln(v ...interface{}) {
if Severity >= LevelDebug {
std.Output(5, fmt.Sprintln(v...), LevelDebug)
}
}
// Print calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
if Severity >= LevelInfo {
std.Output(5, fmt.Sprint(v...), LevelInfo)
}
}
// Printf calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
if Severity >= LevelInfo {
std.Output(5, fmt.Sprintf(format, v...), LevelInfo)
}
}
// Println calls Output to print to the standard logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
if Severity >= LevelInfo {
std.Output(5, fmt.Sprintln(v...), LevelInfo)
}
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Error(v ...interface{}) {
if Severity >= LevelError {
std.Output(5, fmt.Sprint(v...), LevelError)
}
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Errorf(format string, v ...interface{}) {
if Severity >= LevelError {
std.Output(5, fmt.Sprintf(format, v...), LevelError)
}
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func Errorln(v ...interface{}) {
if Severity >= LevelError {
std.Output(5, fmt.Sprintln(v...), LevelError)
}
}
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
if Severity >= LevelFatal {
std.Output(5, fmt.Sprint(v...), LevelFatal)
os.Exit(1)
}
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
if Severity >= LevelFatal {
std.Output(5, fmt.Sprintf(format, v...), LevelFatal)
os.Exit(1)
}
}
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
func Fatalln(v ...interface{}) {
if Severity >= LevelFatal {
std.Output(5, fmt.Sprintln(v...), LevelFatal)
os.Exit(1)
}
}