1
1
package xlog
2
2
3
3
import (
4
- "bytes"
5
4
"io"
6
5
"log/slog"
7
6
"time"
8
7
9
8
"github.com/digineo/texd/internal"
10
9
)
11
10
11
+ // An Option represents a functional configuration option. These are
12
+ // used to configure new logger instances.
12
13
type Option func (* options ) error
13
14
14
15
type options struct {
@@ -19,13 +20,16 @@ type options struct {
19
20
handlerOpts * slog.HandlerOptions
20
21
}
21
22
23
+ // Leveled sets the log level.
22
24
func Leveled (l slog.Level ) Option {
23
25
return func (o * options ) error {
24
26
o .handlerOpts .Level = l
25
27
return nil
26
28
}
27
29
}
28
30
31
+ // LeveledString interprets s (see ParseLevel) and sets the log level.
32
+ // If s is unknown, the error will be revealed with New().
29
33
func LeveledString (s string ) Option {
30
34
return func (o * options ) error {
31
35
l , err := ParseLevel (s )
@@ -37,42 +41,41 @@ func LeveledString(s string) Option {
37
41
}
38
42
}
39
43
44
+ // WriteTo sets the output.
40
45
func WriteTo (w io.Writer ) Option {
41
46
return func (o * options ) error {
42
47
o .output = w
43
48
return nil
44
49
}
45
50
}
46
51
47
- func CaptureOutput () (Option , * bytes.Buffer ) {
48
- var b bytes.Buffer
49
- return func (o * options ) error {
50
- o .output = & b
51
- return nil
52
- }, & b
53
- }
54
-
52
+ // MockClock sets up a canned timestamp.
55
53
func MockClock (t time.Time ) Option {
56
54
return func (o * options ) error {
57
55
o .clock = internal .MockClock (t )
58
56
return nil
59
57
}
60
58
}
61
59
60
+ // WithSource enables source code positions in log messages.
62
61
func WithSource () Option {
63
62
return func (o * options ) error {
64
63
o .handlerOpts .AddSource = true
65
64
return nil
66
65
}
67
66
}
68
67
68
+ // WithAttrReplacer configures an attribute replacer.
69
+ // See (slog.HandlerOptions).ReplaceAtrr for details.
69
70
func WithAttrReplacer (f func (groups []string , a slog.Attr ) slog.Attr ) Option {
70
71
return func (o * options ) error {
71
72
o .handlerOpts .ReplaceAttr = f
72
73
return nil
73
74
}
74
75
}
75
76
77
+ // AsJSON configures a JSONHandler, i.e. log messages will be printed
78
+ // as JSON string.
76
79
func AsJSON () Option {
77
80
return func (o * options ) error {
78
81
o .buildHandler = func (o * options ) slog.Handler {
@@ -82,6 +85,8 @@ func AsJSON() Option {
82
85
}
83
86
}
84
87
88
+ // AsText configures a TextHandler, i.e. the message output is a
89
+ // simple list of key=value pairs with minimal quoting.
85
90
func AsText () Option {
86
91
return func (o * options ) error {
87
92
o .buildHandler = func (o * options ) slog.Handler {
@@ -91,6 +96,8 @@ func AsText() Option {
91
96
}
92
97
}
93
98
99
+ // Discard mutes the logger. See also NewDiscard() for a simpler
100
+ // constructor.
94
101
func Discard () Option {
95
102
return func (o * options ) error {
96
103
o .discard = true
0 commit comments