-
Notifications
You must be signed in to change notification settings - Fork 2
/
components.go
151 lines (126 loc) · 3.03 KB
/
components.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
package log
import "os"
import "strings"
type Level int
const (
LevelDebug Level = 1 << iota
LevelInfo
LevelWarn
LevelError
LevelFatal
)
func MakeLevelWithName(name string) Level {
switch strings.ToLower(name) {
case "info":
return LevelInfo
case "warn":
return LevelWarn
case "error":
return LevelError
case "fatal":
return LevelFatal
default:
return LevelDebug
}
}
// -------------------------------
type MessageFormat string
const (
MessageFormatJSON MessageFormat = "json"
MessageFormatText MessageFormat = "text"
)
// MakeMessageFormat would product MessageFormat with raw string.
// MessageFormatText would be default returning if no matched.
func MakeMessageFormat(raw string) MessageFormat {
switch strings.ToLower(raw) {
case string(MessageFormatJSON):
return MessageFormatJSON
default:
return MessageFormatText
}
}
func (f MessageFormat) isJSON() bool {
return f == MessageFormatJSON
}
// -------------------------------
type TimeFormat string
const (
TimeFormatISO8601 TimeFormat = "iso8601"
TimeFormatSeconds TimeFormat = "seconds"
TimeFormatMillis TimeFormat = "millis"
TimeFormatNanos TimeFormat = "nanos"
)
func MakeTimeFormat(raw string) TimeFormat {
switch strings.ToLower(raw) {
case string(TimeFormatSeconds):
return TimeFormatSeconds
case string(TimeFormatMillis):
return TimeFormatMillis
case string(TimeFormatNanos):
return TimeFormatNanos
default:
return TimeFormatISO8601
}
}
// -------------------------------
type LocalFormat struct {
Format MessageFormat
MessageKey string // 默认为M
TimeKey string // 默认为T
LevelKey string // 默认为L
NameKey string // 默认为N
CallerKey string // 默认为C
// 时间格式
TimeFormat TimeFormat // 默认为TimeFormatISO8601
}
func MakeLocalFormat(msg MessageFormat) LocalFormat {
return LocalFormat{
Format: msg,
MessageKey: "M",
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
TimeFormat: TimeFormatISO8601,
}
}
// -------------------------------
type ConsoleStream string
const (
ConsoleStreamStdout ConsoleStream = "stdout"
ConsoleStreamStderr ConsoleStream = "stderr"
)
func MakeConsoleStream(raw string) ConsoleStream {
switch strings.ToLower(raw) {
case string(ConsoleStreamStderr):
return ConsoleStreamStderr
default:
return ConsoleStreamStdout
}
}
func (s ConsoleStream) stream() *os.File {
switch s {
case ConsoleStreamStderr:
return os.Stderr
default:
return os.Stdout
}
}
// -------------------------------
// Rotation stores configs for the log rotation.
// See more in https://github.com/natefinch/lumberjack/tree/v2.0
type FileRotation struct {
MaxSize int
Compress bool
MaxAge int
MaxBackups int
LocalTime bool
// RotateOnTime enables log rotation based on time.
RotateOnTime bool
// RotatePeriod is the period for log rotation.
// Supports daily(d), hourly(h), minute(m) and second(s).
RotatePeriod string
// RotateAfter sets a value for time based rotation.
// Log file rotates every RotateAfter * RotatePeriod.
RotateAfter int
}