-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
53 lines (44 loc) · 1.2 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
package workflow
import (
"fmt"
"github.com/rs/zerolog/log"
"runtime"
)
type Logger interface {
Debug(msg string, keyvals ...interface{})
Info(msg string, keyvals ...interface{})
Warn(msg string, keyvals ...interface{})
Error(msg string, keyvals ...interface{})
}
type MemoryLogger struct{}
func fileLine() string {
_, file, line, ok := runtime.Caller(2)
if ok {
return fmt.Sprintf("%s:%d", file, line)
} else {
return ""
}
}
func (m MemoryLogger) Debug(msg string, keyvals ...interface{}) {
log.Debug().Interface("keyvals", keyvals).Msg(msg)
}
func (m MemoryLogger) Info(msg string, keyvals ...interface{}) {
log.Info().Str("file", fileLine()).Interface("keyvals", keyvals).Msg(msg)
}
func (m MemoryLogger) Warn(msg string, keyvals ...interface{}) {
log.Warn().Interface("keyvals", keyvals).Msg(msg)
}
func (m MemoryLogger) Error(msg string, keyvals ...interface{}) {
errLog := log.Error().Str("file", fileLine())
for i := 0; i < len(keyvals); i += 2 {
tag := keyvals[i].(string)
value := keyvals[i+1]
if value.(error) != nil {
errLog = errLog.Err(value.(error))
continue
}
errLog = errLog.Str(tag, fmt.Sprintf("%+v", value))
}
errLog.Msg(msg)
}
var _ Logger = (*MemoryLogger)(nil)