Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/slog: Source type is public but Record.source() function is private? #66791

Closed
arianvp opened this issue Apr 11, 2024 · 3 comments
Closed

Comments

@arianvp
Copy link

arianvp commented Apr 11, 2024

Go version

go version go1.22.1 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/arian/Library/Caches/go-build'
GOENV='/Users/arian/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/arian/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/arian/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/nix/store/pi4myqk3ssdk2lp6z05h3yywknlr07i5-go-1.22.1/share/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/nix/store/pi4myqk3ssdk2lp6z05h3yywknlr07i5-go-1.22.1/share/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/arian/Projects/personal/systemd-project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/8f/q1dx480905jf7k40b1wbr2_80000gn/T/go-build711937819=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I am trying to make my own slog.Handler for systemd-journald which has support for linking to source code in logs, so I want to log the slog.Source for each record. However the slog.Record struct does not expose a Source() function to create a slog.Source. there is actually nothing in the slog package that creates a slog.Source

There is a function (r *Record) source() but it's private. Is this a typo? I'd expect this function to be public. Why else would the type be public?

What did you see happen?

There is no r.Source() method

What did you expect to see?

there is a r.Source() method

@seankhliao
Copy link
Member

The package example demonstrates where Source is used: https://pkg.go.dev/log/slog#hdr-Writing_a_handler

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 12, 2024
@arianvp
Copy link
Author

arianvp commented Apr 12, 2024

That example is when you're wrapping an existing Handler.

But if you want to write a handler from scratch (E.g. in my case I want to write a structured logger to systemd-journald) there is no way to create a Source from a Record

Instead I need to copy this function into my handler implementation:

func (r Record) source() *Source {
	fs := runtime.CallersFrames([]uintptr{r.PC})
	f, _ := fs.Next()
	return &Source{
		Function: f.Function,
		File:     f.File,
		Line:     f.Line,
	}
}

which seems a bit silly. Why not expose that function publicly?

@arianvp
Copy link
Author

arianvp commented Apr 12, 2024

Ah I guess I can just use r.PC which is public in my Handler. Nevermind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants