forked from gookit/slog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsugared.go
121 lines (102 loc) · 2.58 KB
/
sugared.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
package slog
import (
"io"
"os"
"github.com/gookit/color"
)
// SugaredLogger definition.
// Is a fast and usable Logger, which already contains
// the default formatting and handling capabilities
type SugaredLogger struct {
*Logger
// Formatter log message formatter. default use TextFormatter
Formatter Formatter
// Output writer
Output io.Writer
// Level for log handling. if log record level <= Level, it will be record.
Level Level
}
// NewStdLogger instance
func NewStdLogger() *SugaredLogger {
return NewSugaredLogger(os.Stdout, DebugLevel).Config(func(sl *SugaredLogger) {
sl.SetName("stdLogger")
sl.CallerSkip += 1
sl.ReportCaller = true
// auto enable console color
sl.Formatter.(*TextFormatter).EnableColor = color.SupportColor()
})
}
// NewSugaredLogger create new SugaredLogger
func NewSugaredLogger(output io.Writer, level Level) *SugaredLogger {
sl := &SugaredLogger{
Level: level,
Output: output,
Logger: New(),
// default value
Formatter: NewTextFormatter(),
}
// NOTICE: use self as an log handler
sl.AddHandler(sl)
return sl
}
// NewJSONSugared create new SugaredLogger with JSONFormatter
func NewJSONSugared(out io.Writer, level Level) *SugaredLogger {
sl := NewSugaredLogger(out, level)
sl.Formatter = NewJSONFormatter()
return sl
}
// Config current logger
func (sl *SugaredLogger) Config(fn func(sl *SugaredLogger)) *SugaredLogger {
fn(sl)
return sl
}
// Configure current logger
func (sl *SugaredLogger) Configure(fn func(sl *SugaredLogger)) *SugaredLogger {
fn(sl)
return sl
}
// Reset the logger
func (sl *SugaredLogger) Reset() {
*sl = *NewSugaredLogger(os.Stdout, DebugLevel)
}
// IsHandling Check if the current level can be handling
func (sl *SugaredLogger) IsHandling(level Level) bool {
return sl.Level.ShouldHandling(level)
}
// Handle log record
func (sl *SugaredLogger) Handle(record *Record) error {
bts, err := sl.Formatter.Format(record)
if err != nil {
return err
}
_, err = sl.Output.Write(bts)
return err
}
// Close all log handlers
func (sl *SugaredLogger) Close() error {
sl.Logger.VisitAll(func(handler Handler) error {
// TIP: must exclude self
if _, ok := handler.(*SugaredLogger); !ok {
err := handler.Close()
if err != nil {
sl.err = err
}
}
return nil
})
return sl.err
}
// Flush all logs. alias of the FlushAll()
func (sl *SugaredLogger) Flush() error {
return sl.FlushAll()
}
// FlushAll all logs
func (sl *SugaredLogger) FlushAll() error {
sl.Logger.VisitAll(func(handler Handler) error {
if _, ok := handler.(*SugaredLogger); !ok {
_ = handler.Flush()
}
return nil
})
return nil
}