Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"fmt"
"log/slog"
"codeberg.org/shimeoki/line"
)
func main() {
ctx := line.With(context.Background(), line.Warn)
defer line.Record(ctx, line.Wrap(slog.Default()), "lined")
line.At(ctx).
Info("status", "line opened").
Warn("important", "something is missing")
line.At(ctx).Error("error", fmt.Errorf("an error happened"))
}
Output:
Index ¶
- func Record(ctx context.Context, lr Liner, msg string) context.Context
- func With(ctx context.Context, quietness Level) context.Context
- type Attr
- type Level
- type Line
- func (ln Line) Add(lvl Level, key string, value any) Line
- func (ln Line) Append(lvl Level, attrs ...Attr) Line
- func (ln Line) Debug(key string, value any) Line
- func (ln Line) Error(key string, value any) Line
- func (ln Line) Info(key string, value any) Line
- func (ln Line) Warn(key string, value any) Line
- type Liner
- type LinerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Record ¶
Record retrieves a Line from ctx to log it in lr with the provided msg. It returns a context.Context without a Line.
If ctx doesn't have a Line, then the function call does nothing and returns the same context.
This function is safe to use by multiple goroutines.
func With ¶
With returns a derived context.Context from ctx with a new Line and specified quietness. This Line is independent, but has copied attributes and severity from the Line in ctx (if there was one).
It is expected to also use Record in the same function where With was called, analogous to Open and Close.
Types ¶
type Level ¶
type Level int
Level is either severity of an event or quietness (event threshold).
Severity is an overall importance of an event or a Line. Literally it means "the highest event level in the line". Higher value means higher event level, therefore higher severity. For example, if a Line has both Debug and Warn events, then the severity is Warn and cannot drop below Warn.
Quietness is a filter for events in a Line. Stored events should have severity equal to or greater than the quietness value. Therefore, resulting line cannot have severity less than the quietness. For example, with Warn quietness, Warn and Error events are stored, while Info and Debug are not.
Values of Level align with the values of slog.Level.
type Line ¶
type Line struct {
// contains filtered or unexported fields
}
Line is a struct for current canonical log line data. It's kept in a context.Context and can be retrieved using At. Line shouldn't be created manually, see With. Zero value is safe to use, but does nothing.
Though Line is used as a value (not passed around as a pointer), it should be treated as such, because of the shared state. In each derived context the state of a Line is shared between the functions to collect [Attr]s through the call stack.
Line is safe to use by multiple goroutines.
func At ¶
At returns a Line from provided ctx.
If a Line wasn't found in ctx, then Line has a no-op state. In this state all method calls are skipped.
func (Line) Add ¶
Add calls Line.Append with lvl and a single Attr.
func (Line) Append ¶ added in v0.2.0
Append attrs with severity lvl to ln. Line is returned to allow method chaining. Append is skipped on empty attrs or no-op ln.
type Liner ¶
type Liner interface {
// Line should log attrs on specified severity level with a msg.
// The implementer should assume attrs are read-only.
Line(ctx context.Context, severity Level, msg string, attrs ...Attr)
}
Liner wraps basic Line method.