Skip to content

refactor: replace zap with slog #2421

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

danielandersson
Copy link

This PR replaces the third-party Zap logging library with Go's standard library slog package for structured logging. This change reduced the number of dependencies while maintaining the same logging functionality and format.

Fixes #2120

Changes

  • Removed dependencies on go.uber.org/zap and go.uber.org/zap/zapcore
  • Added usage of Go's standard library log/slog package
  • Simplified the StdLogger structure by combining infoLog and debugLog into a single stdLog
  • Refactored the StructuredLogger to use slog.Logger instead of Zap's SugaredLogger
  • Modified the NewStructuredLogger function to use slog handlers and configuration
  • Added a new replaceAttr function to maintain compatibility with Google Cloud Logging format
  • Ensured that log messages continue to adhere to the LogEntry format for Google Cloud Logging

@danielandersson danielandersson requested a review from a team as a code owner April 4, 2025 15:07
@jackwotherspoon
Copy link
Collaborator

Thanks @danielandersson 👏

I will take a look at this in the coming days 😄

@jackwotherspoon
Copy link
Collaborator

@hessjcg mind taking a look at this one?

Also @enocom you might also want to take a look as a similar change would probably be added to AlloyDB Proxy 😄

Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

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

Some minor style nits. Otherwise LGTM.

@hessjcg we should manually verify the before and after logs just to be certain this doesn't break the existing structured logging. I think it's correct, but better to double check.

if a.Key == slog.LevelKey {
a.Key = googLvlKey
return a
} else if a.Key == slog.MessageKey {
Copy link
Member

Choose a reason for hiding this comment

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

We can remove all these else usages, since they immediately follow a return statement.

// replaceAttr remaps default Go logging keys to adhere to LogEntry format
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
func replaceAttr(groups []string, a slog.Attr) slog.Attr {
if groups == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Let's invert this check so we don't have to indent the happy path.

https://google.github.io/styleguide/go/decisions#indent-error-flow

)

const (
googLvlKey = "severity"
Copy link
Member

Choose a reason for hiding this comment

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

These are only used in the function below. Let's just inline them.

@danielandersson danielandersson force-pushed the replace-zap-with-slog branch from 615ebd8 to 0946cd0 Compare May 12, 2025 22:05
@danielandersson
Copy link
Author

Some minor style nits. Otherwise LGTM.

@hessjcg we should manually verify the before and after logs just to be certain this doesn't break the existing structured logging. I think it's correct, but better to double check.

Thanks for the review, all of your feedback should have been addressed with 0946cd0.

Copy link
Collaborator

@hessjcg hessjcg left a comment

Choose a reason for hiding this comment

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

Thank you for this excellent change! Approved.

@hessjcg hessjcg enabled auto-merge (squash) May 22, 2025 16:04
@hessjcg
Copy link
Collaborator

hessjcg commented May 22, 2025

/gcbrun

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.

Replace zap with log/slog
4 participants