forked from beatlabs/patron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
61 lines (54 loc) · 1.17 KB
/
factory.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
package zerolog
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"time"
"github.com/beatlabs/patron/log"
"github.com/rs/zerolog"
)
// Create creates a zerolog factory with default settings.
func Create(lvl log.Level) log.FactoryFunc {
zerolog.LevelFieldName = "lvl"
zerolog.MessageFieldName = "msg"
zerolog.TimeFieldFormat = time.RFC3339Nano
zl := zerolog.New(os.Stderr).With().Timestamp().Logger().Hook(sourceHook{skip: 7})
return func(f map[string]interface{}) log.Logger {
return NewLogger(&zl, lvl, f)
}
}
type sourceHook struct {
skip int
}
func (sh sourceHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
k, v, ok := sourceFields(sh.skip)
if !ok {
return
}
e.Str(k, v)
}
func sourceFields(skip int) (key string, src string, ok bool) {
_, file, line, ok := runtime.Caller(skip)
if !ok {
return
}
src = getSource(file, line)
key = "src"
ok = true
return key, src, ok
}
func getSource(file string, line int) (src string) {
if file == "" {
return
}
d, f := filepath.Split(file)
d = path.Base(d)
if d == "." || d == "" {
src = fmt.Sprintf("%s:%d", f, line)
} else {
src = fmt.Sprintf("%s/%s:%d", d, f, line)
}
return src
}