Skip to content

feat(autosync): add structured logging with slog to background sync manager#52

Closed
Tech-Code1 wants to merge 2 commits intoGentleman-Programming:mainfrom
Tech-Code1:feat/autosync-structured-logging
Closed

feat(autosync): add structured logging with slog to background sync manager#52
Tech-Code1 wants to merge 2 commits intoGentleman-Programming:mainfrom
Tech-Code1:feat/autosync-structured-logging

Conversation

@Tech-Code1
Copy link

Summary

The autosync background manager ran completely silently. When cloud sync stopped working, the only way to diagnose the issue was to query the database directly and inspect the degraded state columns.

This PR adds structured logging using Go's standard log/slog package (available since Go 1.21, no new dependencies) at every meaningful lifecycle point in the manager.

What gets logged

Event Level Key fields
Manager started Info target_key, poll_interval, push_batch, pull_batch
Manager stopped Info target_key
Cycle skipped — backoff active Debug backoff_until, consecutive_failures
Cycle skipped — max failures Warn consecutive_failures, max
Lease error Warn error
Lease held by another worker Debug
Push skipped — nothing pending Debug
Pushing mutations Debug count
Per-project batch pushed Debug project, mutations
Push failed Warn error, elapsed
Pull skipped — already up to date Debug last_seq
Pull complete Debug mutations_applied, last_seq
Pull failed Warn error, elapsed
Failure recorded Warn error, consecutive_failures, backoff_until
Sync healthy Info last_sync_at
Cycle complete Debug elapsed

Why slog?

  • Standard library since Go 1.21 — zero new dependencies
  • Structured key/value pairs are machine-readable (works with any slog handler: JSON, logfmt, etc.)
  • Callers can configure the handler — engram users running their own cloud instance can plug in whatever sink they want

Test plan

  • All 25 existing autosync tests pass unchanged
  • Full project builds without errors (go build ./...)

🤖 Generated with Claude Code

…anager

The autosync manager ran completely silently. When cloud sync stopped
working, there was no way to know if the lease was being contested,
how many mutations were pending, or what the backoff state was without
inspecting the database directly.

Add slog logging at key lifecycle points using Go's standard log/slog
package (available since Go 1.21):

- Info on manager start/stop with config summary
- Debug when skipping a cycle due to backoff or lease contention
- Warn when lease acquisition errors occur
- Debug on push with pending mutation count and per-project batch results
- Debug on pull with applied mutation count and final seq cursor
- Warn on push/pull failures with elapsed time
- Warn when recording a failure with consecutive failure count and backoff deadline
- Info when sync returns to healthy state
Copy link
Collaborator

@Alan-TheGentleman Alan-TheGentleman left a comment

Choose a reason for hiding this comment

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

This is useful — the autosync manager was a black box before, and structured logging with proper levels (Info for lifecycle, Warn for failures, Debug for cycle details) is exactly what was needed.

One thing I'd like to see before merging:

  1. Manual prefix "autosync: " in every messageslog supports logger groups (slog.With("component", "autosync") or logger.WithGroup("autosync")) which would give you the same namespacing without repeating the prefix string everywhere. If we add slog to other packages later, we don't want manual prefixes scattered around. Can you use a named logger or slog.With instead? Something like:
logger := slog.Default().With("component", "autosync")
// then: logger.Info("manager started", ...)

This keeps the output structured and avoids the string prefix convention.

Once that's updated, this is good to merge.

…anual prefix

Replace bare slog.XXX("autosync: ...") calls with a component-scoped
logger initialized as slog.Default().With("component", "autosync").
This keeps namespacing consistent and structured without repeating a
string prefix in every log message.
@Alan-TheGentleman
Copy link
Collaborator

Hey @Tech-Code1, thanks for this work! Unfortunately we've removed the cloud packages from the public repo — cloud sync is still under active development in a separate repository. This PR no longer has a target to apply to.

If/when cloud ships publicly, structured logging will definitely be part of it. Appreciate the contribution!

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.

3 participants