-
Notifications
You must be signed in to change notification settings - Fork 75
/
logger.go
58 lines (47 loc) · 1.3 KB
/
logger.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
package logger
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"github.com/cenkalti/log"
)
var handler log.Handler
func init() {
SetHandler(log.NewFileHandler(os.Stderr))
}
// SetHandler changes the global logging handler.
func SetHandler(h log.Handler) {
handler = h
handler.SetFormatter(logFormatter{})
}
// SetLevel sets the logging level to DEBUG on the global handler.
func SetDebug() {
handler.SetLevel(log.DEBUG)
}
// Disable all logging by setting a handler that discards all messages.
func Disable() {
SetHandler(log.NewWriterHandler(io.Discard))
}
// Logger is for logging messages from inside of the program in various logging levels.
type Logger log.Logger
// New returns a new Logger with a name.
// Log messages are prefixed with this name by the default Handler.
func New(name string) Logger {
logger := log.NewLogger(name)
logger.SetLevel(log.DEBUG) // forward all messages to handler
logger.SetHandler(handler)
return logger
}
type logFormatter struct{}
// Format outputs a message like:
// 2014-02-28 18:15:57 [example] INFO somethinfig happened
func (f logFormatter) Format(rec *log.Record) string {
return fmt.Sprintf("%s %-8s [%s] %-8s %s",
fmt.Sprint(rec.Time)[:19],
rec.Level,
rec.LoggerName,
filepath.Base(rec.Filename)+":"+strconv.Itoa(rec.Line),
rec.Message)
}