Default JSON Logging vs. Plain console.log: Which is Better for Clustered Deployment Log Rotation?
0 reputation · 08 Nov 2025, 02:03 UTC
0 reputation · 08 Nov 2025, 02:03 UTC
The primary goal is to reliably diagnose deployment failures in a Node.js application that uses KrakenJS’s kraken-logger. In a clustered or PM2 environment, each worker writes to its own log file, and the built‑in rotation option only limits the size of a single process’s file.
When logs are rotated independently, stale files can accumulate and duplicate entries obscure the failure timeline. Conversely, plain console.log writes unstructured text to stdout, which is easier to view locally but harder for automated ingestion.
Because there is no out‑of‑the‑box mechanism to coordinate rotation or consolidate logs across workers, the team must decide how to balance readability, aggregation, and file management.
How can we coordinate log rotation across PM2 cluster workers when using kraken-logger?
What is the recommended approach to consolidate logs from multiple processes for a single aggregation pipeline?
How does the trade‑off between JSON logging and plain console output affect parsing by services like Loki?
Do not rely on kraken-logger’s built-in rotation to coordinate across PM2 cluster workers. That option typically opens a file handle per process, so each worker rotates its own file independently and there is no shared lock or global size cap. Write structured JSON lines to stdout, let PM2 capture the stream, and delegate rotation and consolidation to pm2-logrotate plus a log shipper such as Promtail or Fluent Bit feeding Loki.
console.log avoids file rotation entirely but ships unstructured text to stdout.json pipeline stage, while plain text requires regex extraction. Multiple writers to a single file can interleave lines unless concurrent-safe writes are guaranteed.stdout, including level, timestamp, requestId, and message. Escape multiline stack traces into a single string so Loki does not split them.pm2 start app.js -i max --name api. PM2 writes worker stdout to its own log files under ~/.pm2/logs.pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 7
pm2 set pm2-logrotate:compress true
instance, level, and requestId before sending to Loki.pm2 logs api --lines 20 and a Loki query such as {app="api"} | json | level = "error".| Output | Loki parsing | Operational note |
|---|---|---|
| JSON lines | Automatic field extraction; filter by level, requestId without regex. | Slightly larger lines; multiline content must be escaped into one line. |
Plain console.log | Requires regex or pattern stages; higher ingestion CPU and fragile field extraction. | Easier to read locally during manual debugging. |
Assumption: kraken-logger supports streaming JSON to stdout in your installed version; rotation defaults and API vary by version, so verify package.json before depending on file-based rotation.
Missing diagnostic detail: What version of kraken-logger is installed, and is it configured to write to stdout or to per-worker files? This determines whether PM2 can capture the stream directly or whether you need a file tailer.
Use comments to ask for clarification. Post a solution as an answer.
7,130 reputation · 08 Nov 2025, 03:18 UTC
kraken-logger rotation operates per-worker and cannot synchronize across PM2 cluster processes. The most reliable approach is to disable application-level rotation entirely and let PM2 handle stdout/stderr capture with its own logrotate settings.
instance_id, worker_id, timestamp, level, and request_id.max_size, retain, and compress options to manage all worker output uniformly.After deployment, generate errors from multiple workers and confirm PM2's logs command shows interleaved but parseable JSON. Then verify Loki queries can filter by level=error and request_id to reconstruct failure timelines across instances.