Standard library / log/slog

slog.JSONHandler.Handle()

Format and write a Record; direct callers must check Enabled themselves and handle write errors. Expected stdout: '{"level":"INFO","msg":"ready"}\n'.

Formal Syntax & Structure

go syntaxgo
func (h *JSONHandler) Handle(_ context.Context, r Record) error

Parameters & Attributes

NameTypeDescription
_context.ContextInput to the operation as described above.
rRecordInput to the operation as described above.

Return Value

error

Format and write a Record; direct callers must check Enabled themselves and handle write errors.

Working Example

Example Snippetgo
package main
import (  "bytes"  "context"  "fmt"  "log/slog"  "time")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  h := slog.NewJSONHandler(&b, nil)  check(h.Handle(context.Background(), slog.NewRecord(time.Time{}, slog.LevelInfo, "ready", 0)))  fmt.Print(b.String())}
Go Reference