slog

package
v1.0.0-beta.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// standard levels
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError

	// LevelExtraDebug is between Debug and Trace. It had extra information that's useful sometimes
	// but not as overwhelming as Trace. e.g. OTel logging is at this level.
	LevelExtraDebug = slog.LevelDebug - 1

	// Trace is the most verbose level. It includes session logging and an enormous amount of detail from
	// buildkit on cache refs and cache queries.
	LevelTrace = slog.LevelDebug - 2
)

Variables

This section is empty.

Functions

func ColorProfileFromContext added in v0.19.6

func ColorProfileFromContext(ctx context.Context) termenv.Profile

OutputWithContextColorMode returns a termenv.Output configured according to the given context's OpenTelemetry baggage.

func ContextWithColorMode added in v0.11.7

func ContextWithColorMode(ctx context.Context, noColor bool) context.Context

ContextWithColorMode enables or disables color mode in the given context's OpenTelemetry baggage.

func ContextWithDebugMode added in v0.11.7

func ContextWithDebugMode(ctx context.Context, debug bool) context.Context

ContextWithDebugMode enables or disables debug mode in the given context's OpenTelemetry baggage.

func Debug

func Debug(msg string, args ...any)

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

func Error

func Error(msg string, args ...any)

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

func ExtraDebug

func ExtraDebug(msg string, args ...any)

func ExtraDebugContext

func ExtraDebugContext(ctx context.Context, msg string, args ...any)

func Info

func Info(msg string, args ...any)

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

func IsDebug added in v0.19.7

func IsDebug(ctx context.Context) bool

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) *slog.JSONHandler

func NewTextHandler

func NewTextHandler(w io.Writer, opts *slog.HandlerOptions) *slog.TextHandler

func SetDefault

func SetDefault(l *Logger)

func Trace

func Trace(msg string, args ...any)

func TraceContext

func TraceContext(ctx context.Context, msg string, args ...any)

func Warn

func Warn(msg string, args ...any)

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

func WithLogger added in v0.19.11

func WithLogger(ctx context.Context, logger *Logger) context.Context

WithLogger returns a new context with the given logger stored in it.

Types

type HandlerOptions added in v0.19.11

type HandlerOptions = slog.HandlerOptions

type Level

type Level = slog.Level

type LineWriter added in v0.19.11

type LineWriter struct {
	// contains filtered or unexported fields
}

LineWriter wraps a Logger and writes each line as a log message at the specified level. It buffers partial lines until a newline is received.

func NewLineWriter added in v0.19.11

func NewLineWriter(logger *Logger, level Level) *LineWriter

NewLineWriter creates an io.Writer that logs each line at the specified level.

func (*LineWriter) Write added in v0.19.11

func (w *LineWriter) Write(p []byte) (n int, err error)

type Logger

type Logger struct {
	*slog.Logger
}

Logger wraps the slog.Logger type with support for a few additional levels

func Default

func Default() *Logger

func FromContext added in v0.19.11

func FromContext(ctx context.Context) *Logger

FromContext returns the Logger stored in the context, or the default logger if none is set.

func GlobalLogger added in v0.19.1

func GlobalLogger(ctx context.Context, name string, attrs ...log.KeyValue) *Logger

GlobalLogger returns a Logger that sends logs to the global span, or the current span if none is configured.

func New

func New(h slog.Handler) *Logger

func PrettyLogger added in v0.11.7

func PrettyLogger(dest io.Writer, profile termenv.Profile, level slog.Level) *Logger

func SpanLogger added in v0.11.7

func SpanLogger(ctx context.Context, name string, attrs ...log.KeyValue) *Logger

SpanLogger returns a Logger that writes to the give context's span logs.

The logger will use the context's baggage to determine the log level and color profile:

- If no-color=true is set in baggage, the profile will be Ascii. Otherwise, it will be ANSI.

- If debug=true is set in baggage, the log level will be Debug. Otherwise, it will be Info.

func With

func With(args ...any) *Logger

func (*Logger) ExtraDebug

func (l *Logger) ExtraDebug(msg string, args ...any)

func (*Logger) ExtraDebugContext

func (l *Logger) ExtraDebugContext(ctx context.Context, msg string, args ...any)

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...any)

func (*Logger) TraceContext

func (l *Logger) TraceContext(ctx context.Context, msg string, args ...any)

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

type Writer

type Writer struct {

	// SyncWarn makes Write wait for lines at WARN level or worse.
	SyncWarn bool
	// contains filtered or unexported fields
}

Writer queues log output for a shared destination (usually stderr) and writes it from one background goroutine, so goroutines that log at the same time do not each wait for every other goroutine's write(2).

Bytes reach the destination in the order Write calls queued them. Each Write is queued whole, so a log line is never split or interleaved with another line written through the Writer. Queued bytes, including the batch being written, are bounded by limit (a single larger line is let through alone): Write blocks while the queue is full, so a destination that cannot keep up slows callers down instead of losing output.

Write returns once its bytes are queued, except for lines whose level is WARN or worse when SyncWarn is set; those wait until they are written, as with a plain locked writer. Flush waits too. A destination write error loses the rest of that batch, as a failed direct write loses its line; callers waiting on those bytes get the error and the count of their bytes that were written.

A Writer runs one goroutine for the life of the process and has no Close: it is meant for the process's own log output.

func NewWriter

func NewWriter(out io.Writer, limit int) *Writer

NewWriter returns a Writer to out that queues at most limit bytes.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush waits until everything queued before the call is done, and returns a destination error from any of it not yet written when Flush was called.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL