Skip to content

ft: v2 redesign on log/slog with slog, zap and zerolog backends - #4

Merged
hanguyenk merged 5 commits into
mainfrom
ft/v2-redesign
Nov 12, 2026
Merged

hanguyenk merged 5 commits into
mainfrom
ft/v2-redesign

Conversation

@hanguyenk

@hanguyenk hanguyenk commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Why did we need it?

v1 had problems that couldn't be fixed without breaking changes:

  • a custom Logger interface over zap and logrus;
  • caller-skip arithmetic that reported the wrong file:line on some call paths;
  • a sync.Once global;
  • backends that disagreed on levels and formats.

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.

  • The API is the standard *slog.Logger. Code that logs depends on log/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 with slog.SetDefault. Config has 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.
  • Backends: slog (the default), zap, and zerolog (new). logrus is dropped.
  • One JSON format for every backend, documented in doc.go and the README, and enforced by a conformance suite.
  • The caller comes from slog.Record.PC, so there's no caller-skip code and the caller is right on every call path.
  • Customization: SetLevel at runtime, ContextWith for request-scoped attributes, WithWriter, and WithMiddleware, which works for every backend.
  • Go bug workaround: the slog backend removes empty groups first, because Go 1.27.4's slog.JSONHandler writes 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;
  • time zone offset +0700 → +07:00;
  • durations are strings like "1.5s" instead of float seconds;
  • errors no longer get an errVerbose field;
  • there is no fatal level;
  • text output is colored only on a terminal.

An unknown level string now makes New fail; v1's zap backend quietly used info. All deployed configs found use only info or debug.

Known limitations (documented)

  • JSON key order differs by backend, and text output looks different per backend. Neither is part of the format guarantee.
  • ContextWith attributes end up inside an open WithGroup.
  • With zerolog, zerolog.SetGlobalLevel(zerolog.Disabled) silences the logger, and write errors go to zerolog.ErrorHandler.

Before services can adopt it

  • kutils/klog wraps v1 and needs a v2 version.
  • kyberswap-dex-lib takes logger.Logger in its public API.
  • onchain-price-service's GetDelegate() use can be replaced by the one-line go-grpc-middleware adapter in the README.

Development tooling

  • golangci-lint, gofumpt and golines are Go tools pinned in tools.mod, run with go tool -modfile=tools.mod. They aren't in go.mod, so modules importing this one don't inherit their dependencies.
  • golangci-lint is pinned to v2.15.0, the version in 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 fmt runs gofumpt and then golines (100 columns). make lint and CI fail on unformatted code, because .golangci.yml enables both formatters.
  • make test and make bench run 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. Running make fmt a second time changes nothing.
  • conformance_test.go logs each case through all three backends and compares the decoded JSON with an expected value. Cases cover:
    • scalars and their extremes, escaping, and NaN/±Inf;
    • durations and times;
    • TextMarshaler, []byte, nil, and errors (fmt.Formatter, json.Marshaler, typed nil);
    • marshal failures and panics;
    • LogValuer;
    • inline, empty and nested-empty groups;
    • With before and after WithGroup;
    • context attributes and custom levels.
  • testing/slogtest passes for every backend, through the full handler chain from New.
  • Caller tests cover:
    • Info, InfoContext, Log, LogAttrs, With, WithGroup and ContextWith;
    • slog.Info after SetDefault, and log.Print;
    • middleware, and console plus file fan-out.
  • Further tests cover:
    • config parsing, defaults and errors;
    • level changes, including a concurrent SetLevel race test;
    • file output and Close;
    • text format, and no ANSI color codes when not writing to a terminal;
    • middleware order;
    • write errors.
  • go test -bench . runs the benchmarks for each backend.

🤖 Generated with Claude Code

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>
@kyber-ci-bot

Copy link
Copy Markdown
Test coverage changes:
Package Before After Diff
github.com/KyberNetwork/logger/v2 0.00% 98.20% ✔️

hanguyenk and others added 4 commits November 12, 2026 15:34
.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>
@hanguyenk
hanguyenk merged commit 9dedb80 into main Nov 12, 2026
4 checks passed
@hanguyenk
hanguyenk deleted the ft/v2-redesign branch November 12, 2026 09:03
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

Successfully merging this pull request may close these issues.

2 participants