Standard library / log/slog

slog.Logger.Enabled()

Ask the handler whether an event at the given level is enabled. Expected stdout: 'false true\n'.

Formal Syntax & Structure

go syntaxgo
func (l *Logger) Enabled(ctx context.Context, level Level) bool

Parameters & Attributes

NameTypeDescription
ctxcontext.ContextContext passed to the handler.
levelLevelSeverity of the event or minimum level.

Return Value

bool

Ask the handler whether an event at the given level is enabled.

Working Example

Example Snippetgo
package main
import (  "context"  "fmt"  "io"  "log/slog")
func main() {  l := slog.New(slog.NewTextHandler(io.Discard, nil))  fmt.Println(l.Enabled(context.Background(), slog.LevelDebug), l.Enabled(context.Background(), slog.LevelInfo))}
Go Reference