-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlog.go
122 lines (101 loc) · 2.97 KB
/
stdlog.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
package log
import (
"errors"
"fmt"
"io"
"io/ioutil"
stdlog "log"
"os"
)
var commonOptions, _ = Options(
WithUTCTimestamp,
WithMicrosecondsTimestamp,
WithSourceLocationShort,
WithLevel(Debug),
WithPrintLevel(Debug),
WithStdlogHandler,
WithStdlogSorter(defaultLogSorter),
)
type stdLogOption interface {
applyStdLog(*stdLogger) error
}
func formatMessage(level Level, format string, value ...interface{}) string {
return fmt.Sprintf(level.String()+": "+format, value...)
}
type stdLogger struct {
level Level
flags flags
printLevel Level
writer io.WriteCloser
logger *stdlog.Logger
stdSorter logSorter
}
// Write satisfies io.Writer interface so that stdLogger can be used as writer for the standard global logger.
func (l *stdLogger) Write(p []byte) (_ int, err error) {
level := l.stdSorter(p)
if level.IsEnabled(l.level) {
err = l.logger.Output(4, formatMessage(level, "%s", string(p)))
}
return
}
func (l *stdLogger) PrintLevel() Level {
return l.printLevel
}
// Close disables and closed the logger, freeing up resources.
func (l *stdLogger) Close() {
l.level = Disabled
// stop using the writer before closing it
l.logger.SetOutput(ioutil.Discard)
_ = l.writer.Close()
}
func (l *stdLogger) Level() Level {
return l.level
}
func (l *stdLogger) Logf(level Level, format string, value ...interface{}) {
if level.IsEnabled(l.level) {
_ = l.logger.Output(3, formatMessage(level, format, value...))
}
}
// New creates a new logger with the specified options.
func New(opt ...Option) (log Log, err error) {
l := &stdLogger{
flags: stdlog.LstdFlags,
}
// apply default options first
if err = commonOptions.applyStdLog(l); err != nil {
err = newConfigError(err)
return
}
// apply any specified options
for _, o := range opt {
if err = o.applyStdLog(l); err != nil {
err = newConfigError(err)
return
}
}
if l.writer == nil {
err = newConfigError(errors.New("no writer given"))
return
}
l.logger = stdlog.New(l.writer, "", int(l.flags))
return Log{logger: l}, nil
}
// NewStderr creates a new logger that logs to stderr. Additional options can be specified using opt.
func NewStderr(opt ...Option) (Log, error) {
return New(options(opt), WithWriter(os.Stderr))
}
// NewStdout creates a new logger that logs to stdout. Additional options can be specified using opt.
func NewStdout(opt ...Option) (Log, error) {
return New(options(opt), WithWriter(os.Stdout))
}
// NewLogfile creates a new logger that logs to the specified file. A file is created
// with permissions specified in perm, if the file does not exist. If the file already
// exists, new records are appended to it. Additional options can be specified using opt.
func NewLogfile(file string, perm os.FileMode, opt ...Option) (log Log, err error) {
var f io.WriteCloser
if f, err = os.OpenFile(file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, perm); err != nil {
err = fmt.Errorf("failed to open log file; error:%w", err)
return
}
return New(options(opt), WithWriter(f))
}