Skip to content

Conversation

@hqpho
Copy link
Contributor

@hqpho hqpho commented Aug 26, 2025

Replaced all instances of log.Println with the slog structured logging library for info and warning level log messages.

Reasons to do this:

  • log.Println has error severity when ingested by GCP, so datcom-mixer logs appear to be full of errors from e.g. the "Mixer ready to serve" statement.
  • Structured logging is recommended by GCP for use with GKE

Steps:

  • Introduced a new internal/log/log.go file to centralize logger setup.
  • Updated cmd/main.go, internal/server/v2/facet/contained_in.go, internal/server/v2/observation/contained_in.go, test/http_memprof/http_memprof.go, and test/setup.go to use slog.Info and slog.Warn instead of log.Println.
  • Configured the logger to include source file and line number information.

Replaced all instances of `log.Println` with the `slog` structured logging library for info and warning level log messages.

- Introduced a new `internal/log/log.go` file to centralize logger setup.
- Updated `cmd/main.go`, `internal/server/v2/facet/contained_in.go`, `internal/server/v2/observation/contained_in.go`, `test/http_memprof/http_memprof.go`, and `test/setup.go` to use `slog.Info` and `slog.Warn` instead of `log.Println`.
- Configured the logger to include source file and line number information.
@hqpho
Copy link
Contributor Author

hqpho commented Aug 26, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step towards modernizing logging by adopting the slog library for structured logging. The changes are well-implemented across the codebase, and centralizing the logger configuration is a good practice. My review focuses on a few areas for improvement within the test utility files: one critical fix for incorrect error logging, and a few suggestions to enhance the context of the new structured logs by including relevant variables, which will improve debuggability.

if os.WriteFile(
path.Join(root, fname), bytes.TrimRight(buf.Bytes(), "\n"), 0644) != nil {
log.Printf("could not write golden files to %s", fname)
slog.Warn("could not write golden files to", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This log is misleading. It's logging the err variable from the encoder.Encode call on line 300, not the error from os.WriteFile. The error from os.WriteFile is not being captured. The surrounding if block should be changed to capture and log the correct error, for example:

if err := os.WriteFile(
        path.Join(root, fname), bytes.TrimRight(buf.Bytes(), "\n"), 0644); err != nil {
    slog.Warn("could not write golden file", "file", fname, "err", err)
}

data, err := marshaller.Marshal(resp)
if err != nil {
log.Printf("marshaller.Marshal(%s) = %s", fname, err)
slog.Warn("marshaller.Marshal()", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The new log message loses the context of which file (fname) failed. It's good practice to include such contextual information in structured logs for easier debugging.

Suggested change
slog.Warn("marshaller.Marshal()", "err", err)
slog.Warn("marshaller.Marshal() failed", "file", fname, "err", err)

jsonByte, err := json.MarshalIndent(rm, "", " ")
if err != nil {
log.Printf("json.MarshalIndent(%s) = %s", fname, err)
slog.Warn("json.MarshalIndent()", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to a previous comment, the filename context (fname) is lost in this log message. Including it would make debugging easier.

Suggested change
slog.Warn("json.MarshalIndent()", "err", err)
slog.Warn("json.MarshalIndent() failed", "file", fname, "err", err)

err = os.WriteFile(path.Join(root, fname), jsonByte, 0644)
if err != nil {
log.Printf("os.WriteFile(%s) = %s", fname, err)
slog.Warn("os.WriteFile", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The filename context (fname) is lost here as well. Please add it to the structured log to provide more context for debugging.

Suggested change
slog.Warn("os.WriteFile", "err", err)
slog.Warn("os.WriteFile failed", "file", fname, "err", err)

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.

1 participant