Skip to content

Commit c64d20d

Browse files
committed
add documentation
1 parent 6b7efce commit c64d20d

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

xlog/attrs.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package xlog
22

33
import "log/slog"
44

5+
// Convenience slog.Attr generators. Allows cluttering imports (no need
6+
// to import both log/slog and xlog).
57
var (
68
String = slog.String
79
Int64 = slog.Int64
@@ -15,14 +17,21 @@ var (
1517
Any = slog.Any
1618
)
1719

20+
// Key used to denote error values.
1821
const ErrorKey = "error"
1922

23+
// ErrorValue holds an error value.
2024
type ErrorValue struct{ error }
2125

26+
// Value extracts the error message.
2227
func (err ErrorValue) Value() slog.Value {
2328
return slog.StringValue(err.Error())
2429
}
2530

31+
// Error constructs a first-class error log attribute.
32+
//
33+
// Not to be confused with (xlog.Logger).Error() or (log/slog).Error(),
34+
// which produce an error-level log message.
2635
func Error(err error) slog.Attr {
2736
return slog.Any(ErrorKey, ErrorValue{err})
2837
}

xlog/options.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package xlog
22

33
import (
4-
"bytes"
54
"io"
65
"log/slog"
76
"time"
87

98
"github.com/digineo/texd/internal"
109
)
1110

11+
// An Option represents a functional configuration option. These are
12+
// used to configure new logger instances.
1213
type Option func(*options) error
1314

1415
type options struct {
@@ -19,13 +20,16 @@ type options struct {
1920
handlerOpts *slog.HandlerOptions
2021
}
2122

23+
// Leveled sets the log level.
2224
func Leveled(l slog.Level) Option {
2325
return func(o *options) error {
2426
o.handlerOpts.Level = l
2527
return nil
2628
}
2729
}
2830

31+
// LeveledString interprets s (see ParseLevel) and sets the log level.
32+
// If s is unknown, the error will be revealed with New().
2933
func LeveledString(s string) Option {
3034
return func(o *options) error {
3135
l, err := ParseLevel(s)
@@ -37,42 +41,41 @@ func LeveledString(s string) Option {
3741
}
3842
}
3943

44+
// WriteTo sets the output.
4045
func WriteTo(w io.Writer) Option {
4146
return func(o *options) error {
4247
o.output = w
4348
return nil
4449
}
4550
}
4651

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.
5553
func MockClock(t time.Time) Option {
5654
return func(o *options) error {
5755
o.clock = internal.MockClock(t)
5856
return nil
5957
}
6058
}
6159

60+
// WithSource enables source code positions in log messages.
6261
func WithSource() Option {
6362
return func(o *options) error {
6463
o.handlerOpts.AddSource = true
6564
return nil
6665
}
6766
}
6867

68+
// WithAttrReplacer configures an attribute replacer.
69+
// See (slog.HandlerOptions).ReplaceAtrr for details.
6970
func WithAttrReplacer(f func(groups []string, a slog.Attr) slog.Attr) Option {
7071
return func(o *options) error {
7172
o.handlerOpts.ReplaceAttr = f
7273
return nil
7374
}
7475
}
7576

77+
// AsJSON configures a JSONHandler, i.e. log messages will be printed
78+
// as JSON string.
7679
func AsJSON() Option {
7780
return func(o *options) error {
7881
o.buildHandler = func(o *options) slog.Handler {
@@ -82,6 +85,8 @@ func AsJSON() Option {
8285
}
8386
}
8487

88+
// AsText configures a TextHandler, i.e. the message output is a
89+
// simple list of key=value pairs with minimal quoting.
8590
func AsText() Option {
8691
return func(o *options) error {
8792
o.buildHandler = func(o *options) slog.Handler {
@@ -91,6 +96,8 @@ func AsText() Option {
9196
}
9297
}
9398

99+
// Discard mutes the logger. See also NewDiscard() for a simpler
100+
// constructor.
94101
func Discard() Option {
95102
return func(o *options) error {
96103
o.discard = true

xlog/xlog.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log/slog"
88
"os"
99
"runtime"
10+
"strings"
1011
"time"
1112
)
1213

@@ -110,16 +111,18 @@ func (log *logger) With(a ...slog.Attr) Logger {
110111
}
111112
}
112113

114+
// ParseLevel tries to convert a (case-insensitive) string into a
115+
// slog.Level. Accepted values are "debug", "info", "warn", "warning",
116+
// "error" and "fatal". Other input will result in err not being nil.
113117
func ParseLevel(s string) (l slog.Level, err error) {
114-
switch s {
115-
case "debug", "DEBUG":
118+
switch strings.ToLower(s) {
119+
case "debug":
116120
l = slog.LevelDebug
117-
case "info", "INFO", "": // make the zero value useful
121+
case "info", "": // make the zero value useful
118122
l = slog.LevelInfo
119-
case "warn", "WARN":
123+
case "warn", "warning":
120124
l = slog.LevelWarn
121-
case "error", "ERROR",
122-
"fatal", "FATAL":
125+
case "error", "fatal":
123126
l = slog.LevelError
124127
default:
125128
err = fmt.Errorf("unknown log level: %q", s)

0 commit comments

Comments
 (0)