Repository navigation
Expand file tree
/
Copy pathbase_logger.go
More file actions
127 lines (113 loc) · 2.8 KB
/
Copy pathbase_logger.go
File metadata and controls
127 lines (113 loc) · 2.8 KB
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
package logging
import (
"context"
"runtime"
"strings"
)
// BaseLogger is the interface that needs to be implemented by client loggers
type BaseLogger interface {
Log(ctx context.Context, level Level, skip int, args ...interface{})
SetLevel(level Level)
GetLevel() Level
}
// Options contain all common options for BaseLoggers
type Options struct {
ReplaceStdLogger bool
JSONLogs bool
Level Level
TimestampFieldName string
LevelFieldName string
CallerInfo bool
CallerFileDepth int
CallerFieldName string
}
// GetDefaultOptions fetches loggers default options
func GetDefaultOptions() Options {
return DefaultOptions
}
// DefaultOptions stores all default options in loggers package
var (
DefaultOptions = Options{
ReplaceStdLogger: false,
JSONLogs: true,
Level: InfoLevel,
TimestampFieldName: "@timestamp",
LevelFieldName: "level",
CallerInfo: true,
CallerFileDepth: 2,
CallerFieldName: "caller",
}
)
// Option defines an option for BaseLogger
type Option func(*Options)
// WithReplaceStdLogger enables/disables replacing std logger
func WithReplaceStdLogger(replaceStdLogger bool) Option {
return func(o *Options) {
o.ReplaceStdLogger = replaceStdLogger
}
}
// WithJSONLogs enables/disables json logs
func WithJSONLogs(json bool) Option {
return func(o *Options) {
o.JSONLogs = json
}
}
// WithTimestampFieldName sets the name of the time stamp field in logs
func WithTimestampFieldName(name string) Option {
return func(o *Options) {
if name != "" {
o.TimestampFieldName = name
}
}
}
// WithLevelFieldName sets the name of the level field in logs
func WithLevelFieldName(name string) Option {
return func(o *Options) {
if name != "" {
o.LevelFieldName = name
}
}
}
// WithCallerInfo enables/disables adding caller info to logs
func WithCallerInfo(callerInfo bool) Option {
return func(o *Options) {
o.CallerInfo = callerInfo
}
}
// WithCallerFileDepth sets the depth of file to use in caller info
func WithCallerFileDepth(depth int) Option {
return func(o *Options) {
if depth > 0 {
o.CallerFileDepth = depth
}
}
}
// WithCallerFieldName sets the name of callerinfo field
func WithCallerFieldName(name string) Option {
return func(o *Options) {
name = strings.TrimSpace(name)
if name != "" {
o.CallerFieldName = name
}
}
}
// FetchCallerInfo fetches function name, file name and line number from stack
func FetchCallerInfo(skip int, depth int) (function string, file string, line int) {
if depth <= 0 {
depth = 2
}
if skip < 0 {
skip = 0
}
pc, file, line, _ := runtime.Caller(skip + 1)
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
depth--
if depth == 0 {
file = file[i+1:]
break
}
}
}
return runtime.FuncForPC(pc).Name(), file, line
}