forked from morita-kuma/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
183 lines (145 loc) · 3.86 KB
/
metadata.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package golog
import (
"runtime"
"time"
)
// SourceLine
type SourceLine = int
// SourceFile
type SourceFile = string
// LoggerName
type LoggerName = string
// UnixTime
type UnixTime = int64
// LogEventMetadata
type LogEventMetadata struct {
LogLevel LogLevel
UnixTime UnixTime
SourceFile SourceFile
SourceLine SourceLine
LoggerName LoggerName
MetadataFormatter
MetadataConfig
}
// MetadataConfig
type MetadataConfig struct {
IsEnabledLogLevel bool
IsEnabledTime bool
IsEnabledSourceFile bool
IsEnabledSourceLine bool
IsEnabledLoggerName bool
}
// NewDefaultMetadataConfig
func NewDefaultMetadataConfig() MetadataConfig {
return MetadataConfig{
IsEnabledLoggerName: true,
IsEnabledLogLevel: true,
IsEnabledSourceFile: true,
IsEnabledSourceLine: true,
IsEnabledTime: true,
}
}
// GetLogLevel returns log level formatted by LogLevelFormatter
// if metadata config is disabled, returns empty string
func (metadata *LogEventMetadata) GetLogLevel() string {
if metadata.IsEnabledLogLevel == false {
return ""
}
return metadata.LogLevelFormatter(metadata.LogLevel)
}
// GetTime returns time formatted by TimeFormatter
// if metadata config is disabled, returns empty string
func (metadata *LogEventMetadata) GetTime() string {
if metadata.IsEnabledTime == false {
return ""
}
return metadata.TimeFormatter(metadata.UnixTime)
}
// GetSourceFile returns package name formatted by SourceFileFormatter
// if metadata config is disabled, returns empty string
func (metadata *LogEventMetadata) GetSourceFile() string {
if metadata.IsEnabledSourceFile == false {
return ""
}
return metadata.SourceFileFormatter(metadata.SourceFile)
}
// GetSourceFile returns package name formatted by SourceFileFormatter
// if metadata config is disabled, returns empty string
func (metadata *LogEventMetadata) GetLoggerName() string {
if metadata.IsEnabledLoggerName == false {
return ""
}
return metadata.LoggerNameFormatter(metadata.LoggerName)
}
// GetSourceLine returns line formatted by SourceLineFormatter
// if metadata config is disabled, returns empty string
func (metadata *LogEventMetadata) GetSourceLine() string {
if metadata.IsEnabledSourceLine == false {
return ""
}
return metadata.SourceLineFormatter(metadata.SourceLine)
}
// setLoggerName
func (metadata *LogEventMetadata) setLoggerName(loggerName string) {
if metadata == nil {
return
}
if metadata.IsEnabledLoggerName == true {
metadata.LoggerName = loggerName
}
}
// setLogLevel
func (metadata *LogEventMetadata) setLogLevel(logLevel LogLevel) {
if metadata == nil {
return
}
if metadata.IsEnabledLogLevel == true {
metadata.LogLevel = logLevel
}
}
// setSource
func (metadata *LogEventMetadata) setSource(skip int) {
if metadata == nil {
return
}
if metadata.IsEnabledSourceLine == true || metadata.IsEnabledSourceFile == true {
_, file, line, _ := runtime.Caller(skip)
metadata.SourceLine = line
metadata.SourceFile = file
}
}
// setTime
func (metadata *LogEventMetadata) setTime() {
if metadata == nil {
return
}
if metadata.IsEnabledTime == true {
metadata.UnixTime = time.Now().Unix()
}
}
// NewLogEventMetadata
func NewLogEventMetadata(config *MetadataConfig, formatter *MetadataFormatter) LogEventMetadata {
metadata := LogEventMetadata{
MetadataFormatter: NewDefaultMetadataFormatter(),
MetadataConfig:NewDefaultMetadataConfig(),
}
if config != nil {
metadata.MetadataConfig = *config
}
if formatter != nil {
metadata.MetadataFormatter = *formatter
}
return metadata
}
// newLogEventMetadata
func newDefaultLogEventMetadata(loggerName string, logLevel LogLevel) *LogEventMetadata {
metadata := LogEventMetadata{
MetadataFormatter: NewDefaultMetadataFormatter(),
MetadataConfig:NewDefaultMetadataConfig(),
}
metadata.setLogLevel(logLevel)
metadata.setLoggerName(loggerName)
metadata.setSource(2)
metadata.setTime()
return &metadata
}