-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
159 lines (138 loc) · 3.22 KB
/
logger.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
package logger
import (
"fmt"
"io"
"log"
"github.com/fasthttpd/fasthttpd/pkg/config"
"github.com/valyala/fasthttp"
)
// Logger is the interface that defines the methods to logging.
type Logger interface {
fasthttp.Logger
Rotator
io.Closer
LogLogger() *log.Logger
}
// NewLogger creates a new logger.
func NewLogger(cfg config.Log) (Logger, error) {
if cfg.Output == "" {
return NilLogger, nil
}
out, err := SharedRotator(cfg.Output, cfg.Rotation)
if err != nil {
return nil, err
}
l, err := newLogger(out, cfg)
if err != nil {
out.Close() //nolint:errcheck
return nil, err
}
return l, nil
}
// NewLoggerWriter returns a new logger with out.
func NewLoggerWriter(cfg config.Log, out io.Writer) (Logger, error) {
return newLogger(&NopRotator{Writer: out}, cfg)
}
type logger struct {
*log.Logger
rotator Rotator
}
var _ Logger = (*logger)(nil)
func newLogger(out Rotator, cfg config.Log) (*logger, error) {
flgs := 0
for _, flg := range cfg.Flags {
switch flg {
case "date":
flgs |= log.Ldate
case "time":
flgs |= log.Ltime
case "microsecond":
flgs |= log.Lmicroseconds
case "utc":
flgs |= log.LUTC
case "msgprefix":
flgs |= log.Lmsgprefix
default:
return nil, fmt.Errorf("unknown flag: %s", flg)
}
}
return &logger{
Logger: log.New(out, cfg.Prefix, flgs),
rotator: out,
}, nil
}
// Rotate rotate log stream.
func (l *logger) Rotate() error {
if l.rotator != nil {
return l.rotator.Rotate()
}
return nil
}
// Write writes to log stream.
func (l *logger) Write(p []byte) (int, error) {
if l.rotator != nil {
return l.rotator.Write(p)
}
return 0, nil
}
// Close closes log stream.
func (l *logger) Close() error {
if l.rotator != nil {
if err := l.rotator.Close(); err != nil {
return err
}
l.rotator = nil
}
return nil
}
func (l *logger) LogLogger() *log.Logger {
return l.Logger
}
type nilLogger struct{}
var (
NilLogLogger = log.New(io.Discard, "", 0)
NilLogger nilLogger
_ Logger = (*nilLogger)(nil)
)
func (nilLogger) Printf(format string, args ...interface{}) {}
func (nilLogger) Rotate() error { return nil }
func (nilLogger) Write([]byte) (int, error) { return 0, nil }
func (nilLogger) Close() error { return nil }
func (nilLogger) LogLogger() *log.Logger { return NilLogLogger }
// LoggerDelegator can delegate the Logger functions.
type LoggerDelegator struct {
PrintfFunc func(format string, args ...interface{})
RotateFunc func() error
WriteFunc func([]byte) (int, error)
CloseFunc func() error
LogLoggerFunc func() *log.Logger
}
func (l *LoggerDelegator) Printf(format string, args ...interface{}) {
if l.PrintfFunc != nil {
l.PrintfFunc(format, args...)
}
}
func (l *LoggerDelegator) Rotate() error {
if l.RotateFunc != nil {
return l.RotateFunc()
}
return nil
}
func (l *LoggerDelegator) Write(p []byte) (int, error) {
if l.WriteFunc != nil {
return l.WriteFunc(p)
}
return 0, nil
}
func (l *LoggerDelegator) Close() error {
if l.CloseFunc != nil {
return l.CloseFunc()
}
return nil
}
func (l *LoggerDelegator) LogLogger() *log.Logger {
if l.LogLoggerFunc != nil {
return l.LogLoggerFunc()
}
return NilLogLogger
}