forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
79 lines (68 loc) · 1.97 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
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 {
FormattableHandler
IsHandling(level Level) bool
}
/********************************************************************************
* Common parts for handler
********************************************************************************/
// LevelWithFormatter struct definition
//
// - support set log formatter
// - only support set one log level
type LevelWithFormatter struct {
Formattable
// Level for log message. if current level >= Level will log message
Level Level
}
// NewLvFormatter create new instance
func NewLvFormatter(lv Level) *LevelWithFormatter {
return &LevelWithFormatter{Level: lv}
}
// 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 {
Formattable
// Levels for log message
Levels []Level
}
// NewLvsFormatter create new instance
func NewLvsFormatter(levels []Level) *LevelsWithFormatter {
return &LevelsWithFormatter{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
}