Skip to content

feat(logs): diagnostic log in a rotating file, readable by the admin - #25

Merged
Digit4w merged 5 commits into
devfrom
feat/diagnostic-logs
Sep 14, 2026
Merged

Digit4w merged 5 commits into
devfrom
feat/diagnostic-logs

Conversation

@Digit4w

@Digit4w Digit4w commented Sep 14, 2026 •

Copy link
Copy Markdown
Owner

Contraparte: Digit4w/watchpile-client#36 — mergear este primeiro.

The diagnostic log was the item blocking the next pre-release: users who hit a problem had nothing to send. Most failures from providers, imports, art and updates were swallowed by catch blocks, background work logged through console.*, and nothing reached disk.

  • Logger as a module (lib/logger.ts), writing to stdout and to logs/watchpile.log next to the database, rotated in-process at 5 MB × 3 (no worker-thread transport, which breaks in packaged Electron).
  • Secrets redacted at the emitter on every line: by value (resolved credentials, OAuth tokens, session secret) and by name (cookie, authorization, password, credential query params). The per-request line no longer carries headers — the session cookie used to be logged on every request.
  • Swallowed failures now log: warn when a third party fails, error when it is ours, never the request URL.
  • GET /api/logs and /api/logs/download, admin only, read backwards and stop at 1,000 lines; the download is JSON Lines.
  • Fix: the art cache defaulted to ./data/art relative to the cwd, outside the /data volume in the container. It now derives from the database directory, like the log.

No contract break: routes are new. Brief 5.1 was corrected by the owner (log goes to stdout and to a file in the volume).

🤖 Generated with Claude Code

WATCHPILE_ART_CACHE_PATH defaulted to ./data/art, relative to the
process cwd. In the Docker image that is /app/data/art, outside the
/data volume, so cached artwork was silently lost on every container
recreation; in packaged Electron it landed wherever the cwd was. The
comment next to the variable claimed the opposite.

The variable is now an optional override. Without it the cache lives
in art/ next to WATCHPILE_DB_PATH, the one path both hosts declare,
via besideDatabase() in lib/data-dir.ts, which the log file will
reuse. READMEs: art cache is no longer "in the future", and the
variable is listed in the configuration table.
The pino instance lived inside the HTTP middleware, so only route
handlers could log; background work used console.* and nothing
reached disk. lib/logger.ts is now the root logger, and the
middleware is one of its consumers.

- Two destinations: stdout (pino-pretty as an in-process stream in
  dev, never a transport, which breaks in packaged Electron) and
  logs/watchpile.log next to the database, rotated in-process at
  5 MB x 3 files (WATCHPILE_LOG_PATH, _MAX_MB, _FILES overrides).
- Secrets are redacted on every line at the emitter (streamWrite):
  by value for resolved provider credentials, OAuth tokens and the
  session secret, and by name for cookie/authorization/password
  keys and credential query params.
- The per-request line no longer carries headers (the session
  cookie was logged on every request). Only 5xx (error), 4xx and
  responses over 1s (info) reach the file at the default level;
  the rest drops to debug.
- The startup line goes through the logger instead of console.log.
Provider, art, import and update failures became typed states for
the UI and left no trace, so a user reporting "search is broken"
had nothing to send. Every such catch now emits through the root
logger, never with the request URL (it can carry a key); the
redaction in streamWrite scrubs whatever else slips through.

- warn when a third party fails: provider search/detail/units
  refused, unreachable or not JSON; art image refused, not an
  image or out of range; token exchange; import sources going
  down (the cause used to be lost in ImportFailure); update feed
  and asset download; an unreadable *_FILE credential (path
  only).
- error when the failure is ours: unexpected import, fill,
  refresh and enrich jobs; art warm; update download, check and
  install; corrupt JSON in notification params and import jobs.
- debug for a job that did not start because one is already
  running, which is the expected path.
- import logs "import finished", and a typed failure logs its kind
  (params stay out: they carry the username).
- console.* is gone from the server outside env.ts, which runs
  before the logger exists.

logger.spec.ts writes through the root logger and reads the file,
so removing the redaction hook fails a test (checked by removing
it).
GET /api/logs returns the newest lines of the file lib/logger.ts
writes, and GET /api/logs/download returns every kept file as JSON
Lines. Both sit behind adminMiddleware for the whole feature, like
storage: the log is the instance's, not a user's.

- The read walks files newest first, backwards, and stops at 1,000
  lines, so a poll costs the slice and not the 15 MB.
- level=all|warn|error is cumulative by severity. before= loads
  older lines, after= follows new ones. The cursor is the pino
  time, which survives rotation (line numbers do not); two lines
  in the same millisecond at a page boundary may repeat or skip.
- Every line goes through redactLine again on the way out, so a
  secret registered after the line was written is still scrubbed.
- The download is named by the server
  (watchpile-log-YYYY-MM-DD.jsonl) and never cached.
- The download handler casts its response: @hono/zod-openapi types
  application/x-ndjson as a malformed JSON media type (never), and
  declaring another type would make openapi.json lie about the
  file. The e2e test checks status, headers and body instead.
New section "O log de diagnóstico" (root logger, rotation,
redaction, the admin read/download feature and seven invariants),
the lib files in the tree, and the container bullet now says
stdout and file, following the brief 5.1 correction.
@Digit4w
Digit4w merged commit df28b06 into dev Sep 14, 2026
3 checks passed
@Digit4w
Digit4w deleted the feat/diagnostic-logs branch September 14, 2026 19:23
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