Repository navigation
ft: v2 redesign on log/slog with slog, zap and zerolog backends - #4
Merged
Merged
Conversation
v2 is a new module, github.com/KyberNetwork/logger/v2. The logging API is the standard *slog.Logger; this package configures it and adapts the backend that encodes and writes records. - Config: backend (slog, zap, zerolog), level, format (json, text), caller, console and a rotated file, with json/yaml/mapstructure tags. New has no global side effects and reports every invalid field. - One JSON format for every backend, enforced by a conformance suite. testing/slogtest passes for each backend. - Callers come from Record.PC, so they are right on every call path. - Runtime level changes, ContextWith for request-scoped attributes, WithWriter and WithMiddleware for customization. - The slog backend prunes empty groups, working around invalid JSON from Go 1.27's slog.JSONHandler. - logrus is dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test coverage changes:
|
.golangci.yml keeps the standard linters and enables the gofumpt formatter, so `golangci-lint run` in CI also rejects unformatted code. The Makefile adds fmt, lint, test and bench targets, and warns when the local golangci-lint differs from the v2.15.0 that CI uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mechanical output of gofumpt followed by golines (max 100 columns), with long slog calls wrapped by hand so key/value pairs stay on one line. TestContextAttrsNilContext now passes a nil context variable instead of a nil literal, which removes the staticcheck nolint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tools are declared in tools.mod and run with `go tool -modfile=tools.mod`, so they need no install and don't add dependencies to go.mod, which modules importing this one would inherit. golangci-lint is pinned to v2.15.0, the version CI runs, and gofumpt (v0.12.0) and golines (v0.16.0) to the versions it embeds, so `make fmt` produces exactly what CI checks. .golangci.yml enables golines alongside gofumpt. `make fmt` runs gofumpt and then golines, the order golangci-lint applies them in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why did we need it?
v1 had problems that couldn't be fixed without breaking changes:
Loggerinterface over zap and logrus;file:lineon some call paths;sync.Onceglobal;v2 is a new module,
github.com/KyberNetwork/logger/v2, so services can migrate one at a time with v1 and v2 side by side.*slog.Logger. Code that logs depends onlog/slog, never on this package or a backend, so switching backends is a config change.logger.New(Config, ...Option)builds the logger. It sets no globals; callers opt in withslog.SetDefault.Confighas backend, level, format, caller, console and a rotated file, and carries json, yaml and mapstructure tags. The zero value works. All invalid fields are reported in one error, and the log file is opened at startup.doc.goand the README, and enforced by a conformance suite.slog.Record.PC, so there's no caller-skip code and the caller is right on every call path.SetLevelat runtime,ContextWithfor request-scoped attributes,WithWriter, andWithMiddleware, which works for every backend.slog.JSONHandlerwrites invalid JSON ({"a":1"b":2}) after a group whose attributes are all empty.Breaking changes
The README has a full v1→v2 migration table. The JSON output differs from v1's zap output in these ways:
ts→time;+0700→+07:00;"1.5s"instead of float seconds;errVerbosefield;fatallevel;An unknown level string now makes
Newfail; v1's zap backend quietly used info. All deployed configs found use onlyinfoordebug.Known limitations (documented)
ContextWithattributes end up inside an openWithGroup.zerolog.SetGlobalLevel(zerolog.Disabled)silences the logger, and write errors go tozerolog.ErrorHandler.Before services can adopt it
logger.Loggerin its public API.GetDelegate()use can be replaced by the one-line go-grpc-middleware adapter in the README.Development tooling
tools.mod, run withgo tool -modfile=tools.mod. They aren't ingo.mod, so modules importing this one don't inherit their dependencies.ci.yaml. gofumpt (v0.12.0) and golines (v0.16.0) are pinned to the versions golangci-lint embeds, so local formatting matches CI's check.make fmtruns gofumpt and then golines (100 columns).make lintand CI fail on unformatted code, because.golangci.ymlenables both formatters.make testandmake benchrun the tests and the benchmarks.Related Issue
None.
How Has This Been Tested?
go test -race -vet=off ./...passes. This is the command CI runs.make lint(golangci-lint v2.15.0, standard linters plus the gofumpt and golines formatters) reports 0 issues. Runningmake fmta second time changes nothing.conformance_test.gologs each case through all three backends and compares the decoded JSON with an expected value. Cases cover:TextMarshaler,[]byte, nil, and errors (fmt.Formatter,json.Marshaler, typed nil);LogValuer;Withbefore and afterWithGroup;testing/slogtestpasses for every backend, through the full handler chain fromNew.Info,InfoContext,Log,LogAttrs,With,WithGroupandContextWith;slog.InfoafterSetDefault, andlog.Print;SetLevelrace test;Close;go test -bench .runs the benchmarks for each backend.🤖 Generated with Claude Code