Skip to content

Commit

Permalink
add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmke committed Oct 28, 2024
1 parent 21628c6 commit a722865
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
9 changes: 9 additions & 0 deletions xlog/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xlog

import "log/slog"

// Convenience slog.Attr generators. Allows cluttering imports (no need
// to import both log/slog and xlog).
var (
String = slog.String
Int64 = slog.Int64
Expand All @@ -15,14 +17,21 @@ var (
Any = slog.Any
)

// Key used to denote error values.
const ErrorKey = "error"

// ErrorValue holds an error value.
type ErrorValue struct{ error }

// Value extracts the error message.
func (err ErrorValue) Value() slog.Value {
return slog.StringValue(err.Error())
}

// Error constructs a first-class error log attribute.
//
// Not to be confused with (xlog.Logger).Error() or (log/slog).Error(),
// which produce an error-level log message.
func Error(err error) slog.Attr {
return slog.Any(ErrorKey, ErrorValue{err})
}
25 changes: 16 additions & 9 deletions xlog/options.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package xlog

import (
"bytes"
"io"
"log/slog"
"time"

"github.com/digineo/texd/internal"
)

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

type options struct {
Expand All @@ -19,13 +20,16 @@ type options struct {
handlerOpts *slog.HandlerOptions
}

// Leveled sets the log level.
func Leveled(l slog.Level) Option {
return func(o *options) error {
o.handlerOpts.Level = l
return nil
}
}

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

// WriteTo sets the output.
func WriteTo(w io.Writer) Option {
return func(o *options) error {
o.output = w
return nil
}
}

func CaptureOutput() (Option, *bytes.Buffer) {
var b bytes.Buffer
return func(o *options) error {
o.output = &b
return nil
}, &b
}

// MockClock sets up a canned timestamp.
func MockClock(t time.Time) Option {
return func(o *options) error {
o.clock = internal.MockClock(t)
return nil
}
}

// WithSource enables source code positions in log messages.
func WithSource() Option {
return func(o *options) error {
o.handlerOpts.AddSource = true
return nil
}
}

// WithAttrReplacer configures an attribute replacer.
// See (slog.HandlerOptions).ReplaceAtrr for details.
func WithAttrReplacer(f func(groups []string, a slog.Attr) slog.Attr) Option {
return func(o *options) error {
o.handlerOpts.ReplaceAttr = f
return nil
}
}

// AsJSON configures a JSONHandler, i.e. log messages will be printed
// as JSON string.
func AsJSON() Option {
return func(o *options) error {
o.buildHandler = func(o *options) slog.Handler {
Expand All @@ -82,6 +85,8 @@ func AsJSON() Option {
}
}

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

// Discard mutes the logger. See also NewDiscard() for a simpler
// constructor.
func Discard() Option {
return func(o *options) error {
o.discard = true
Expand Down
15 changes: 9 additions & 6 deletions xlog/xlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -110,16 +111,18 @@ func (log *logger) With(a ...slog.Attr) Logger {
}
}

// ParseLevel tries to convert a (case-insensitive) string into a
// slog.Level. Accepted values are "debug", "info", "warn", "warning",
// "error" and "fatal". Other input will result in err not being nil.
func ParseLevel(s string) (l slog.Level, err error) {
switch s {
case "debug", "DEBUG":
switch strings.ToLower(s) {
case "debug":
l = slog.LevelDebug
case "info", "INFO", "": // make the zero value useful
case "info", "": // make the zero value useful
l = slog.LevelInfo
case "warn", "WARN":
case "warn", "warning":
l = slog.LevelWarn
case "error", "ERROR",
"fatal", "FATAL":
case "error", "fatal":
l = slog.LevelError
default:
err = fmt.Errorf("unknown log level: %q", s)
Expand Down

0 comments on commit a722865

Please sign in to comment.