-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhandler.go
173 lines (148 loc) · 4.04 KB
/
handler.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
package slog
import "io"
//
// Handler interface
//
// Handler interface definition
type Handler interface {
// Closer Close handler.
// You should first call Flush() on close logic.
// Refer the FileHandler.Close() handle
io.Closer
// Flush and sync logs to disk file.
Flush() error
// IsHandling Checks whether the given record will be handled by this handler.
IsHandling(level Level) bool
// Handle a log record.
//
// All records may be passed to this method, and the handler should discard
// those that it does not want to handle.
Handle(*Record) error
}
// LevelFormattable support limit log levels and provide formatter
type LevelFormattable interface {
Formattable
IsHandling(level Level) bool
}
// FormattableHandler interface
type FormattableHandler interface {
Handler
Formattable
}
/********************************************************************************
* Common parts for handler
********************************************************************************/
// LevelWithFormatter struct definition
//
// - support set log formatter
// - only support set max log level
type LevelWithFormatter struct {
FormattableTrait
// Level max for log message. if current level <= Level will log message
Level Level
}
// NewLvFormatter create new LevelWithFormatter instance
func NewLvFormatter(maxLv Level) *LevelWithFormatter {
return &LevelWithFormatter{Level: maxLv}
}
// SetMaxLevel set max level for log message
func (h *LevelWithFormatter) SetMaxLevel(maxLv Level) {
h.Level = maxLv
}
// IsHandling Check if the current level can be handling
func (h *LevelWithFormatter) IsHandling(level Level) bool {
return h.Level.ShouldHandling(level)
}
// LevelsWithFormatter struct definition
//
// - support set log formatter
// - support setting multi log levels
type LevelsWithFormatter struct {
FormattableTrait
// Levels for log message
Levels []Level
}
// NewLvsFormatter create new instance
func NewLvsFormatter(levels []Level) *LevelsWithFormatter {
return &LevelsWithFormatter{Levels: levels}
}
// SetLimitLevels set limit levels for log message
func (h *LevelsWithFormatter) SetLimitLevels(levels []Level) {
h.Levels = levels
}
// IsHandling Check if the current level can be handling
func (h *LevelsWithFormatter) IsHandling(level Level) bool {
for _, l := range h.Levels {
if l == level {
return true
}
}
return false
}
// LevelMode define level mode
type LevelMode uint8
// String return string value
func (m LevelMode) String() string {
switch m {
case LevelModeList:
return "list"
case LevelModeMax:
return "max"
default:
return "unknown"
}
}
const (
// LevelModeList use level list for limit record write
LevelModeList LevelMode = iota
// LevelModeMax use max level limit log record write
LevelModeMax
)
// LevelHandling struct definition
type LevelHandling struct {
// level check mode. default is LevelModeList
lvMode LevelMode
// max level for log message. if current level <= Level will log message
maxLevel Level
// levels limit for log message
levels []Level
}
// SetMaxLevel set max level for log message
func (h *LevelHandling) SetMaxLevel(maxLv Level) {
h.lvMode = LevelModeMax
h.maxLevel = maxLv
}
// SetLimitLevels set limit levels for log message
func (h *LevelHandling) SetLimitLevels(levels []Level) {
h.lvMode = LevelModeList
h.levels = levels
}
// IsHandling Check if the current level can be handling
func (h *LevelHandling) IsHandling(level Level) bool {
if h.lvMode == LevelModeMax {
return h.maxLevel.ShouldHandling(level)
}
for _, l := range h.levels {
if l == level {
return true
}
}
return false
}
// LevelFormatting wrap level handling and log formatter
type LevelFormatting struct {
LevelHandling
FormatterWrapper
}
// NewMaxLevelFormatting create new instance with max level
func NewMaxLevelFormatting(maxLevel Level) *LevelFormatting {
lf := &LevelFormatting{}
lf.SetMaxLevel(maxLevel)
return lf
}
// NewLevelsFormatting create new instance with levels
func NewLevelsFormatting(levels []Level) *LevelFormatting {
lf := &LevelFormatting{}
lf.SetLimitLevels(levels)
return lf
}