Standard library / log/slog

slog.NewTextHandler()

Create a built-in key=value text handler writing to the supplied writer. Expected stdout: 'level=INFO msg=ready\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
wio.WriterWriter receiving log records.
opts*HandlerOptionsHandler options; nil selects defaults.

Return Value

*TextHandler

Create a built-in key=value text handler writing to the supplied writer.

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.NewTextHandler(&b, nil)  check(h.Handle(context.Background(), slog.NewRecord(time.Time{}, slog.LevelInfo, "ready", 0)))  fmt.Print(b.String())}
Go Reference