Documentation
¶
Index ¶
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func DebugContextf(ctx context.Context, format string, args ...any)
- func Debugf(format string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func ErrorContextf(ctx context.Context, format string, args ...any)
- func Errorf(format string, args ...any)
- func Fatal(msg string, args ...any)
- func FatalContext(ctx context.Context, msg string, args ...any)
- func FatalContextf(ctx context.Context, format string, args ...any)
- func Fatalf(format string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func InfoContextf(ctx context.Context, format string, args ...any)
- func Infof(format string, args ...any)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func WarnContextf(ctx context.Context, format string, args ...any)
- func Warnf(format string, args ...any)
- func WithLogger(ctx context.Context, logger *Logger) context.Context
- func WithValues(ctx context.Context, args ...any) context.Context
- type Handler
- type Logger
- func DefaultLogger() *Logger
- func FromContext(ctx context.Context) *Logger
- func New(h slog.Handler) *Logger
- func NewLogger(l *slog.Logger) *Logger
- func NewLoggerWithContext(ctx context.Context, l *slog.Logger) *Logger
- func NewWithContext(ctx context.Context, h slog.Handler) *Logger
- func With(args ...any) *Logger
- func (l *Logger) Base() *slog.Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Fatal(msg string, args ...any)
- func (l *Logger) FatalContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) FatalContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Fatalf(format string, args ...any)
- func (l *Logger) Handler() slog.Handler
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContextf(ctx context.Context, format string, args ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DebugContext ¶
DebugContext calls DebugContext on the context logger.
func DebugContextf ¶
DebugContextf calls DebugContextf on the context logger. If a Logger is found in the context, it will be used.
func ErrorContext ¶
ErrorContext calls ErrorContext on the context logger.
func ErrorContextf ¶
ErrorContextf calls ErrorContextf on the context logger.
func FatalContext ¶ added in v1.3.1
FatalContext calls ErrorContext on the context logger, then exits.
func FatalContextf ¶ added in v1.3.1
FatalContextf calls ErrorContextf on the context logger, then exits.
func InfoContext ¶
InfoContext calls InfoContext on the context logger. If a Logger is found in the context, it will be used.
func InfoContextf ¶
InfoContextf calls InfoContextf on the context logger. If a Logger is found in the context, it will be used.
func WarnContext ¶
WarnContext calls WarnContext on the context logger. If a Logger is found in the context, it will be used.
func WarnContextf ¶
WarnContextf calls WarnContextf on the context logger. If a Logger is found in the context, it will be used.
func WithValues ¶
With returns a new context with the given values. Values are expected to be key-value pairs, where the key is a string. e.g. WithValues(ctx, "foo", "bar", "baz", 1) If a value already exists, it is overwritten. If an odd number of arguments are provided, With panics.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a slog.Handler that adds context values to the log record. Values are added via WithValues.
Example ¶
package main
import (
"context"
"log/slog"
"os"
"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slogtest"
)
func main() {
log := slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// Remove time for repeatable results
ReplaceAttr: slogtest.RemoveTime,
})))
ctx := context.Background()
ctx = clog.WithValues(ctx, "foo", "bar")
log.InfoContext(ctx, "hello world", slog.Bool("baz", true))
}
Output: level=INFO msg="hello world" baz=true foo=bar
func NewHandler ¶
NewHandler configures a new context aware slog handler. If h is nil, the default slog handler is used.
type Logger ¶
Logger implements a wrapper around slog.Logger that adds formatter functions (e.g. Infof, Errorf)
Example ¶
package main
import (
"context"
"log/slog"
"os"
"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slogtest"
)
func main() {
log := clog.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// Remove time for repeatable results
ReplaceAttr: slogtest.RemoveTime,
})))
log = log.With("a", "b")
ctx := clog.WithLogger(context.Background(), log)
// Grab logger from context and use
// Note: this is a formatter aware method, not an slog.Attr method.
clog.FromContext(ctx).With("foo", "bar").Infof("hello %s", "world")
// Package level context loggers are also aware
clog.ErrorContext(ctx, "asdf", slog.Bool("baz", true))
}
Output: level=INFO msg="hello world" a=b foo=bar level=ERROR msg=asdf a=b baz=true
func DefaultLogger ¶
func DefaultLogger() *Logger
DefaultLogger returns a new logger that uses the default slog.Logger.
func FromContext ¶
Example (PreserveContext) ¶
package main
import (
"context"
"log/slog"
"os"
"github.com/chainguard-dev/clog"
"github.com/chainguard-dev/clog/slogtest"
)
func main() {
log := clog.NewLogger(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
// Remove time for repeatable results
ReplaceAttr: slogtest.RemoveTime,
}))).With("foo", "bar")
ctx := clog.WithLogger(context.Background(), log)
// Previous context values are preserved when using FromContext
clog.FromContext(ctx).Info("hello world")
}
Output: level=INFO msg="hello world" foo=bar
func New ¶
New returns a new logger that wraps the given slog.Handler.
func NewLogger ¶
NewLogger returns a new logger that wraps the given slog.Logger with the default context.
func NewLoggerWithContext ¶ added in v1.3.1
NewLoggerWithContext returns a new logger that wraps the given slog.Logger.
func NewWithContext ¶ added in v1.3.1
NewWithContext returns a new logger that wraps the given slog.Handler using the given context.
func (*Logger) Debug ¶ added in v1.7.0
Debug logs at LevelDebug with the given message and treats the args as key/value pairs to form log message attributes.
func (*Logger) DebugContextf ¶
DebugContextf logs at LevelDebug with the given context, format and arguments.
func (*Logger) Error ¶ added in v1.7.0
Error logs at LevelError with the given message and treats the args as key/value pairs to form log message attributes.
func (*Logger) ErrorContextf ¶
ErrorContextf logs at LevelError with the given context, format and arguments.
func (*Logger) FatalContext ¶ added in v1.3.1
FatalContext logs at LevelError with the given context and message, then exits.
func (*Logger) FatalContextf ¶ added in v1.3.1
FatalContextf logs at LevelError with the given context, format and arguments, then exits.
func (*Logger) Fatalf ¶ added in v1.3.1
Fatalf logs at LevelError with the given format and arguments, then exits.
func (*Logger) Handler ¶
Handler returns the underlying slog.Handler.
func (*Logger) Info ¶ added in v1.7.0
Info logs at LevelInfo with the given message and treats the args as key/value pairs to form log message attributes.
func (*Logger) InfoContextf ¶
InfoContextf logs at LevelInfo with the given context, format, and arguments.
func (*Logger) Warn ¶ added in v1.7.0
Warn logs at LevelWarn with the given message and treats the args as key/value pairs to form log message attributes.
func (*Logger) WarnContextf ¶
WarnContextf logs at LevelWarn with the given context, format and arguments.
func (*Logger) With ¶
With calls Logger.With on the logger.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
handler
command
|
|
|
logger
command
|
|
|
Package slag provides a method for setting the log level from the command line.
|
Package slag provides a method for setting the log level from the command line. |
|
Package slogtest provides utilities for emitting test logs using clog.
|
Package slogtest provides utilities for emitting test logs using clog. |