-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlog.go
132 lines (119 loc) · 3.42 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
package loom
import (
"io"
"log"
"os"
"strings"
kitlog "github.com/go-kit/kit/log"
kitlevel "github.com/go-kit/kit/log/level"
)
type Logger struct {
kitlog.Logger
}
func NewFilter(next kitlog.Logger, options ...kitlevel.Option) *Logger {
return &Logger{kitlevel.NewFilter(next, options...)}
}
var (
AllowDebug = kitlevel.AllowDebug
AllowInfo = kitlevel.AllowInfo
AllowWarn = kitlevel.AllowWarn
AllowError = kitlevel.AllowError
Allow = func(level string) kitlevel.Option {
switch level {
case "debug":
return AllowDebug()
case "info":
return AllowInfo()
case "warn":
return AllowWarn()
case "error":
return AllowError()
default:
return nil
}
}
)
var msgKey = "_msg"
// Info logs a message at level Info.
func (l *Logger) Info(msg string, keyvals ...interface{}) {
lWithLevel := kitlevel.Info(l)
args := append([]interface{}{msgKey, msg}, keyvals...)
if err := kitlog.With(lWithLevel).Log(args...); err != nil {
errLogger := kitlevel.Error(l)
kitlog.With(errLogger, msgKey, msg).Log("err", err)
}
}
// Debug logs a message at level Debug.
func (l *Logger) Debug(msg string, keyvals ...interface{}) {
lWithLevel := kitlevel.Debug(l)
args := append([]interface{}{msgKey, msg}, keyvals...)
if err := kitlog.With(lWithLevel).Log(args...); err != nil {
errLogger := kitlevel.Error(l)
errLogger.Log("err", err)
}
}
// Error logs a message at level Error.
func (l *Logger) Error(msg string, keyvals ...interface{}) {
lWithLevel := kitlevel.Error(l)
args := append([]interface{}{msgKey, msg}, keyvals...)
if err := kitlog.With(lWithLevel).Log(args...); err != nil {
args = append([]interface{}{"err", err}, args...)
lWithLevel.Log(args)
}
}
// Warn logs a message at level Debug.
func (l *Logger) Warn(msg string, keyvals ...interface{}) {
lWithLevel := kitlevel.Warn(l)
args := append([]interface{}{msgKey, msg}, keyvals...)
if err := kitlog.With(lWithLevel).Log(args...); err != nil {
errLogger := kitlevel.Error(l)
kitlog.With(errLogger, msgKey, msg).Log("err", err)
}
}
func NewLoomLogger(loomLogLevel, dest string) *Logger {
w := MakeFileLoggerWriter(loomLogLevel, dest)
logTr := func(w io.Writer) kitlog.Logger {
fmtLogger := kitlog.NewLogfmtLogger(kitlog.NewSyncWriter(w))
return kitlog.With(fmtLogger, "ts", kitlog.DefaultTimestampUTC)
}
return MakeLoomLogger(loomLogLevel, w, logTr)
}
func MakeLoomLogger(logLevel string, w io.Writer, tr func(w io.Writer) kitlog.Logger) *Logger {
loggerFunc := func(w io.Writer) *Logger {
if len(logLevel) == 0 {
logLevel = "info"
}
baseLogger := kitlog.With(tr(w), "module", "loom")
return &Logger{
NewFilter(baseLogger, Allow(logLevel)),
}
}
return loggerFunc(w)
}
func MakeFileLoggerWriter(loomLogLevel, dest string) io.Writer {
if dest == "" {
dest = "file://-"
}
destParts := strings.Split(dest, "://")
if len(destParts) != 2 {
log.Fatalf("Invalid log destination specification %s", dest)
}
// Keep below code as reference for syslog later
// case "syslog":
// sysLog, err := syslog.Dial("tcp", destParts[1],
// syslogLevel(loomLogLevel)|syslog.LOG_DAEMON, "loom")
// if err != nil {
// log.Fatal(err)
// }
// Default = kitsyslog.NewSyslogLogger(sysLog, loggerFunc, kitsyslog.PrioritySelectorOption)
if destParts[1] == "-" {
return os.Stderr
} else {
log.Printf("Sending logs to %s\n", dest)
f, err := os.OpenFile(destParts[1], os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
return f
}
}