Skip to content

log/slog: TextHandler, JSONHandler: incorrect docs #73943

Closed
@08d2

Description

@08d2

Go version

go1.24

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/08d2/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/08d2/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/3j/jnfkmqxn2pq4r58r0dcqg1k00000gn/T/go-build1706432072=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/08d2/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/08d2'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.24.2/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/08d2/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.24.2/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.2'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

https://pkg.go.dev/log/slog#TextHandler.Handle

If the Record's level is zero, the level is omitted. Otherwise, the key is "level" and the value of Level.String is output.

https://pkg.go.dev/log/slog#JSONHandler.Handle

If the Record's level is zero, the level is omitted. Otherwise, the key is "level" and the value of Level.String is output.

--

https://go.dev/play/p/gPX5U_TilI6

package main

import (
	"context"
	"log/slog"
	"os"
	"time"
)

func main() {
	th := slog.NewTextHandler(os.Stderr, nil)
	th.Handle(context.TODO(), slog.NewRecord(time.Now().UTC(), 0, "message 1", 0))
	jh := slog.NewJSONHandler(os.Stderr, nil)
	jh.Handle(context.TODO(), slog.NewRecord(time.Now().UTC(), 0, "message 2", 0))
}

Handle gets Record values with level == 0 in both cases.

What did you see happen?

time=2009-11-10T23:00:00.000Z level=INFO msg="message 1"
{"time":"2009-11-10T23:00:00Z","level":"INFO","msg":"message 2"}

What did you expect to see?

time=2009-11-10T23:00:00.000Z level=INFO msg="message 1"
{"time":"2009-11-10T23:00:00Z","level":"INFO","msg":"message 2"}

The level value is not omitted. Which is correct, I think! The docs just need to be fixed.

Metadata

Metadata

Assignees

Labels

DocumentationIssues describing a change to documentation.NeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions