-
Notifications
You must be signed in to change notification settings - Fork 4
/
stdlogger.go
149 lines (124 loc) · 3.96 KB
/
stdlogger.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
package golog
import (
"context"
"fmt"
)
// StdLogger is a representation of the standard Logger
type StdLogger struct {
Writer Writer
Fields Fields
Decorators Decorators
Checkers Checkers
}
// New returns a StdLogger which writes starting from the given Level to the given Writer
func New(w Writer, options ...Option) StdLogger {
l := StdLogger{
Writer: w,
}
for _, o := range options {
l = o.Apply(l)
}
return l
}
// WithDecorator returns a new StdLogger appending the given extra Decorators
func (l StdLogger) WithDecorator(ds ...Decorator) StdLogger {
return StdLogger{
Writer: l.Writer,
Fields: l.Fields,
Decorators: append(l.Decorators, ds...),
Checkers: l.Checkers,
}
}
// WithCheckers returns a new StdLogger appending the given extra Checkers
func (l StdLogger) WithCheckers(cs ...Checker) StdLogger {
return StdLogger{
Writer: l.Writer,
Fields: l.Fields,
Decorators: l.Decorators,
Checkers: append(l.Checkers, cs...),
}
}
// Debug writes a log with the DEBUG Level
func (l StdLogger) Debug(ctx context.Context, msg Message) {
l.log(ctx, DEBUG, msg)
}
// CheckDebug returns a CheckedLogger and a guard
// When the guard is true and the CheckDebug is called a log with the DEBUG Level is written
func (l StdLogger) CheckDebug(ctx context.Context, msg Message) (CheckedLogger, bool) {
return l.check(ctx, DEBUG, msg)
}
// Info writes a log with the INFO Level
func (l StdLogger) Info(ctx context.Context, msg Message) {
l.log(ctx, INFO, msg)
}
// CheckInfo returns a CheckedLogger and a guard
// When the guard is true and the CheckInfo is called a log with the INFO Level is written
func (l StdLogger) CheckInfo(ctx context.Context, msg Message) (CheckedLogger, bool) {
return l.check(ctx, INFO, msg)
}
// Warn writes a log with the WARN Level
func (l StdLogger) Warn(ctx context.Context, msg Message) {
l.log(ctx, WARN, msg)
}
// CheckWarn returns a CheckedLogger and a guard
// When the guard is true and the CheckWarn is called a log with the WARN Level is written
func (l StdLogger) CheckWarn(ctx context.Context, msg Message) (CheckedLogger, bool) {
return l.check(ctx, WARN, msg)
}
// Error writes a log with the ERROR Level
func (l StdLogger) Error(ctx context.Context, msg Message) {
l.log(ctx, ERROR, msg)
}
// CheckError returns a CheckedLogger and a guard
// When the guard is true and the CheckError is called a log with the ERROR Level is written
func (l StdLogger) CheckError(ctx context.Context, msg Message) (CheckedLogger, bool) {
return l.check(ctx, ERROR, msg)
}
// Fatal writes a log with the FATAL Level
// Fatal also panic with the given message
func (l StdLogger) Fatal(ctx context.Context, msg Message) {
l.log(ctx, FATAL, msg)
if err := l.Writer.Flush(); err != nil {
msg = fmt.Sprintf("%s: %s", err, msg)
}
panic(msg)
}
// CheckFatal returns a CheckedLogger and a guard
// When the guard is true and the with is called a log with the FATAL Level is written
// CheckFatal will also panic with the given message
func (l StdLogger) CheckFatal(ctx context.Context, msg Message) (CheckedLogger, bool) {
return l.check(ctx, FATAL, msg)
}
// With returns a new Logger appending the given extra Fields
func (l StdLogger) With(fields ...Field) Logger {
return StdLogger{
Writer: l.Writer,
Fields: append(l.Fields, fields...),
Decorators: l.Decorators,
Checkers: l.Checkers,
}
}
func (l StdLogger) log(ctx context.Context, lvl Level, msg Message) {
var e Entry = NewStdEntry(ctx, lvl, msg, l.Fields)
for _, c := range l.Checkers {
if !c.Check(e) {
return
}
}
for _, d := range l.Decorators {
e = d.Decorate(e)
}
l.Writer.WriteEntry(e)
}
func (l StdLogger) check(ctx context.Context, lvl Level, msg Message) (CheckedLogger, bool) {
var e Entry = NewStdEntry(ctx, lvl, msg, l.Fields)
for _, c := range l.Checkers {
if !c.Check(e) {
return NoopCheckedLogger{}, false
}
}
for _, d := range l.Decorators {
e = d.Decorate(e)
}
return StdCheckedLogger{Writer: l.Writer, Entry: e}, true
}