forked from morita-kuma/golog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_formatter.go
70 lines (55 loc) · 1.74 KB
/
metadata_formatter.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
package golog
import (
"strconv"
"path/filepath"
"time"
)
// TimeFormatter
type TimeFormatter = func(UnixTime) string
// LogLevelFormatter
type LogLevelFormatter = func(level LogLevel) string
// SourceLineFormatter
type SourceLineFormatter = func(line SourceLine) string
// SourceFileFormatter
type SourceFileFormatter = func(packageName SourceFile) string
// LoggerNameFormatter
type LoggerNameFormatter = func(loggerName LoggerName) string
// MetadataFormatter
type MetadataFormatter struct {
LogLevelFormatter LogLevelFormatter
TimeFormatter TimeFormatter
SourceFileFormatter SourceFileFormatter
SourceLineFormatter SourceLineFormatter
LoggerNameFormatter LoggerNameFormatter
}
// NewDefaultMetadataFormatter
func NewDefaultMetadataFormatter() MetadataFormatter {
// DefaultLogLevelFormatter
var defaultLogLevelFormatter = func(logLevel LogLevel) string {
return logLevel.String()
}
// DefaultTimeFormatter
var defaultTimeFormatter = func(unixTime UnixTime) string {
return time.Unix(unixTime, 0).Format(time.RFC3339)
}
// DefaultLineFormatter
var defaultSourceLineFormatter = func(sourceLine SourceLine) string {
return strconv.FormatInt(int64(sourceLine), 10)
}
// DefaultSourceFileFormatter
var defaultSourceFileFormatter = func(sourceFile SourceFile) string {
_, fileName := filepath.Split(sourceFile)
return fileName
}
// DefaultLoggerNameFormatter
var defaultLoggerNameFormatter = func(loggerName LoggerName) string {
return loggerName
}
return MetadataFormatter{
LogLevelFormatter: defaultLogLevelFormatter,
TimeFormatter: defaultTimeFormatter,
SourceLineFormatter: defaultSourceLineFormatter,
SourceFileFormatter: defaultSourceFileFormatter,
LoggerNameFormatter: defaultLoggerNameFormatter,
}
}