Skip to content

Add structured logging with slog#19327

Merged
mhamza15 merged 25 commits into
vitessio:mainfrom
mhamza15:slog
Feb 15, 2026
Merged

Add structured logging with slog#19327
mhamza15 merged 25 commits into
vitessio:mainfrom
mhamza15:slog

Conversation

@mhamza15
Copy link
Copy Markdown
Collaborator

@mhamza15 mhamza15 commented Feb 8, 2026

Description

Adds support for JSON structured logging with slog. To enable, pass --log-structured and optionally a --log-level debug|info|warn|error (default info). glog is still the default unless slog is explicitly enabled with the flags.

We still use a global logging package go/vt/log to make the migration easier. In the future, each component should be passed in a logger.

The global functions in the log package now check if structured logging is enabled. If so, passes the message to slog. If not, passes it to glog as normal.

The API changes from Info(args ...any) and Infof(args ...any) to Info(msg string, attrs ...slog.Attr). Call sites were migrated using ast-grep as so:

  • Info(...) => Info(fmt.Sprint(...))
  • Infof(...) => Info(fmt.Sprintf(...))
  • ...and so on for other levels and Depth variants.

Call sites are now free to incrementally add additional attributes to the message.

Example JSON output:

$ ./bin/vttablet --log-structured
{"time":"2026-01-22T16:13:49.136782-05:00","level":"INFO","source":{"function":"vitess.io/vitess/go/vt/servenv.Init","file":"vitess.io/vitess/go/vt/servenv/servenv_unix.go","line":58},"msg":"Version: 24.0.0-SNAPSHOT (Git revision bba55101eeb6be4ce10914eec14daee3008189c7 branch 'structured-logging-slog') built on Thu Jan 22 15:59:46 EST 2026 by mhamza@mhamza-psc.local using go1.25.6 darwin/arm64"}
{"time":"2026-01-22T16:13:49.136913-05:00","level":"ERROR","source":{"function":"vitess.io/vitess/go/vt/topo.Open","file":"vitess.io/vitess/go/vt/topo/server.go","line":261},"msg":"topo-global-server-address must be configured"}

If structured attributes are passed, but structured logging is not enabled, the attributes are outputted through glog as key=value pairs:

$ ./bin/vttablet
E0207 12:31:03.237133   25625 server.go:263] topo-global-server-address must be configured key1=value1 key2=value2

The plan is for structured logging to be the default in v24, and glog to be deprecated. In v25, glog will be removed.

Benchmark quick links:

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

AI Disclosure

Adds support for JSON structured logging with `slog`. To enable, pass `--log-structured` and optionally a `--log-level debug|info|warn|error` (default info). `glog` is still the default unless `slog` is explicitly enabled with the flags.

We still use a global logging package `go/vt/log` to make the migration easier. In the future, each component should be passed in a logger.

The global functions in the log package now check if structured logging is enabled. If so, passes the message to `slog`. If not, passes it to `glog` as normal.

Call sites were migrated using ast-grep as so:

- `Info(...)` => `Info(fmt.Sprint(...))`
- `Infof(...)` => `Info(fmt.Sprintf(...))`
- ...and so on for other levels and `Depth` variants.

Call sites are now free to incrementally add additional attributes to the message.

Example JSON output:

```console
$ ./bin/vttablet --log-structured
{"time":"2026-01-22T16:13:49.136782-05:00","level":"INFO","source":{"function":"vitess.io/vitess/go/vt/servenv.Init","file":"vitess.io/vitess/go/vt/servenv/servenv_unix.go","line":58},"msg":"Version: 24.0.0-SNAPSHOT (Git revision bba5510 branch 'structured-logging-slog') built on Thu Jan 22 15:59:46 EST 2026 by mhamza@mhamza-psc.local using go1.25.6 darwin/arm64"}
{"time":"2026-01-22T16:13:49.136913-05:00","level":"ERROR","source":{"function":"vitess.io/vitess/go/vt/topo.Open","file":"vitess.io/vitess/go/vt/topo/server.go","line":261},"msg":"topo-global-server-address must be configured"}
```

Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
@mhamza15 mhamza15 self-assigned this Feb 8, 2026
@github-actions github-actions Bot added the Component: Documentation docs related issues/PRs label Feb 8, 2026
@github-actions github-actions Bot added this to the v24.0.0 milestone Feb 8, 2026
@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 8, 2026
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented Feb 8, 2026

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Comment thread go/vt/log/log.go
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much the only real change. The API is changed to match slog's, and internal helpers decide whether to log with slog or glog depending on the configured flags.

Comment thread go/vt/log/prefixed.go
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the original prefix logger from log.go, extracted to its own file and changed to the new slog API. This should eventually be removed and consumers (VTOrc) should use structured attributes instead.

@mhamza15 mhamza15 added Type: Enhancement Logical improvement (somewhere between a bug and feature) Benchmark me Add label to PR to run benchmarks Component: General Changes throughout the code base and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required Component: Documentation docs related issues/PRs labels Feb 8, 2026
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented Feb 8, 2026

Hello! 👋

This Pull Request is now handled by arewefastyet. The current HEAD and future commits will be benchmarked.

You can find the performance comparison on the arewefastyet website.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 8, 2026

Codecov Report

❌ Patch coverage is 29.57746% with 800 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.91%. Comparing base (b004c09) to head (a62178b).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/mysqlctl/mysqld.go 8.97% 71 Missing ⚠️
go/vt/tlstest/tlstest.go 5.40% 70 Missing ⚠️
go/cmd/vtbackup/cli/vtbackup.go 0.00% 29 Missing ⚠️
go/mysql/conn.go 18.75% 26 Missing ⚠️
go/stats/statsd/statsd.go 0.00% 21 Missing ⚠️
go/vt/log/log.go 57.44% 20 Missing ⚠️
go/cmd/vtcombo/cli/vschema_watcher.go 0.00% 19 Missing ⚠️
go/vt/binlog/binlogplayer/binlog_player.go 24.00% 19 Missing ⚠️
go/vt/servenv/grpc_server.go 16.66% 15 Missing ⚠️
go/vt/servenv/servenv.go 25.00% 15 Missing ⚠️
... and 153 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19327      +/-   ##
==========================================
- Coverage   69.95%   69.91%   -0.05%     
==========================================
  Files        1610     1612       +2     
  Lines      216056   216191     +135     
==========================================
+ Hits       151133   151140       +7     
- Misses      64923    65051     +128     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mhamza15 mhamza15 removed the Component: Documentation docs related issues/PRs label Feb 12, 2026
Comment thread go/vt/log/flags.go Outdated
}
utils.SetFlagVar(fs, &flagVal, "log-rotate-max-size", "size in bytes at which logs are rotated (glog.MaxSize)")

fs.BoolVar(&logStructured, "log-structured", true, "enable structured logging")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhamza15 I suggest we make it clear to the user the structure is JSON

Copy link
Copy Markdown
Contributor

@timvaillancourt timvaillancourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one help-text suggestion

Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
@timvaillancourt
Copy link
Copy Markdown
Contributor

@mhamza15 how does this work with file based logging? One thing glog provides users is logging to disk directly, as well as file-rotation, etc. I believe that was built-in to glog

Do we plan to support an equivalent feature for users that enable structured logging, but were used to logging to a file? Sorry if this is in the diff, GitHub and I are having problems reading the change

@mhamza15
Copy link
Copy Markdown
Collaborator Author

@mhamza15 how does this work with file based logging? One thing glog provides users is logging to disk directly, as well as file-rotation, etc. I believe that was built-in to glog

Do we plan to support an equivalent feature for users that enable structured logging, but were used to logging to a file? Sorry if this is in the diff, GitHub and I are having problems reading the change

I intentionally left out that functionality. My reasoning is that Vitess should log to stderr and it should be the responsibility of the operator to pipe that output where they need, plus rotate log files. In Kubernetes, that is pretty much handled for you. In bare-metal/VMs, systemd or other tools can handle that for you as well.

Open to pushback though if anyone feels strongly, but my suggestion is to go this route 🙇‍♂️ .

@timvaillancourt
Copy link
Copy Markdown
Contributor

@mhamza15 makes sense

I'd lean towards we should support logging to disk for structured logging, because I know some bare-metal users that rely (or did rely) on direct logging to disk. So I expect retooling for logging to be a surprise to some. But it's not a must I suppose

Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
@mhamza15 mhamza15 merged commit 1bbce5d into vitessio:main Feb 15, 2026
108 of 112 checks passed
@mhamza15 mhamza15 deleted the slog branch February 15, 2026 20:31
@promptless
Copy link
Copy Markdown
Contributor

promptless Bot commented Feb 15, 2026

📝 Documentation updates detected!

Updated existing suggestion: Changelog entry for structured logging with slog (already in main)


Tip: Set up a GitHub Issues trigger in Projects to enable @Promptless mentions in issues 🐙

@promptless
Copy link
Copy Markdown
Contributor

promptless Bot commented Feb 15, 2026

📝 Documentation updates detected!

Updated existing suggestion: Document structured logging with slog


Tip: Set up a GitHub Issues trigger in Projects to enable @Promptless mentions in issues 🐙

mhamza15 added a commit to mhamza15/vitess that referenced this pull request Feb 15, 2026
Signed-off-by: Mohamed Hamza <mhamza@fastmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Benchmark me Add label to PR to run benchmarks Component: Build/CI Component: Documentation docs related issues/PRs Component: Observability Pull requests that touch tracing/metrics/monitoring Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants