PM2 Logs Cheat Sheet: --lines, --nostream, flush & Rotation
Need the last 100 lines of a PM2 app without locking your terminal, or want to clear overgrown log files on a server? Here are the exact pm2 logs, pm2 flush, and pm2-logrotate commands verified against the official PM2 documentation.
Most Used pm2 logs Commands
# Show last 100 lines of all apps and keep streaming
pm2 logs --lines 100
# Show last 100 lines of one app and EXIT (no live tail)
pm2 logs api --lines 100 --nostream
# Show only stderr (error logs) for one app
pm2 logs api --err --lines 100
# Show only stdout for one app
pm2 logs api --out --lines 100
# Stream logs from apps matching a regex
pm2 logs "/^api-/"
# Stream live logs in JSON format
pm2 logs --json
# Clear all PM2 application log files
pm2 flushBy default, pm2 logs prints the last 15 lines of each process's stdout and stderr logs and then stays attached to stream new lines.
pm2 logs Flags Reference
Syntax: pm2 logs [options] [id|name|namespace]
| Flag | What It Does | Example |
|---|---|---|
--lines <n> | Output the last N lines instead of the default 15 | pm2 logs api --lines 200 |
--nostream | Print the requested lines and exit without tailing | pm2 logs api --lines 100 --nostream |
--err | Show only standard error (stderr) logs | pm2 logs api --err |
--out | Show only standard output (stdout) logs | pm2 logs api --out |
--json | Output live log entries as JSON objects | pm2 logs --json |
--format | Formatted log output | pm2 logs --format |
--raw | Raw output without PM2 prefixes | pm2 logs api --raw |
--timestamp [format] | Add timestamps (default YYYY-MM-DD-HH:mm:ss) | pm2 logs --timestamp |
--highlight [value] | Highlight matching text in the output | pm2 logs --highlight ERROR |
Tip for scripting & grep: Always add
--nostreamwhen pipingpm2 logsintogrep,tail, or CI checks—otherwise the command waits indefinitely for new log lines:bashpm2 logs api --lines 500 --nostream | grep -i "ECONNREFUSED"
What pm2 logs --json Outputs
Each streamed line is emitted as a JSON object with PM2 metadata:
{
"message": "Server listening on port 3000\n",
"timestamp": "2026-12-06T10:15:38.896Z",
"type": "out",
"process_id": 0,
"app_name": "api"
}type is out, err, or PM2.
Where PM2 Stores Log Files
By default, PM2 writes all application logs to:
$HOME/.pm2/logs/Inside that directory, each process gets separate stdout and stderr files:
~/.pm2/logs/<app-name>-out.log~/.pm2/logs/<app-name>-error.log~/.pm2/pm2.log(PM2 daemon's own log)
You can override file locations when starting an app with pm2 start:
# Separate stdout and stderr files
pm2 start app.js --name api -o /var/log/api-out.log -e /var/log/api-err.log
# Combined log file with timestamps
pm2 start app.js --name api -l /var/log/api.log --time
# Merge cluster-mode instances into one file instead of splitting by instance ID
pm2 start app.js -i max --merge-logsFlushing and Reloading PM2 Logs
To empty current application log files managed by PM2:
# Flush logs for all apps
pm2 flush
# Flush logs for a single app (by name or ID)
pm2 flush apiIf an external tool (like Linux logrotate) rotates files out from under PM2, tell PM2 to reopen its log file descriptors:
pm2 reloadLogsAutomatic Log Rotation with pm2-logrotate
PM2 does not rotate logs out of the box—busy Node.js apps can fill a disk if left unmanaged. Install the official pm2-logrotate module using pm2 install (not npm install):
pm2 install pm2-logrotateInspect active settings with pm2 conf, and adjust them with pm2 set pm2-logrotate:<key> <value>:
pm2 set pm2-logrotate:max_size 50M
pm2 set pm2-logrotate:retain 14
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'| Setting | Default | Description |
|---|---|---|
max_size | 10M | Rotate when a log file exceeds this size (K, M, G) |
retain | 30 | Number of rotated archive files to keep (in addition to active file) |
compress | false | Compress rotated logs with gzip |
dateFormat | YYYY-MM-DD_HH-mm-ss | Timestamp format appended to rotated filenames |
rotateModule | true | Also rotate logs of PM2 modules |
workerInterval | 30 | How often (in seconds, min 1) the worker checks file sizes |
rotateInterval | 0 0 * * * | Cron schedule forcing rotation (default: daily at midnight) |
TZ | System time | Standard tz database timezone for rotated file timestamps |
Prefer native Linux logrotate instead of a Node worker? Generate /etc/logrotate.d/pm2-<user> in one command:
sudo pm2 logrotate -u youruserBrowse PM2 Logs in a Browser UI with Logdy
When you are debugging multiple PM2 processes at once, scanning interleaved pm2 logs output in a terminal gets noisy quickly. Pipe pm2 logs --json into Logdy to get a local browser UI at http://127.0.0.1:8080 with automatic columns (app_name, type, process_id, message) and one-click filtering:
# Install Logdy (macOS or Linux)
brew install logdy
# or: curl https://logdy.dev/install.sh | sh
# Stream all PM2 apps into Logdy
pm2 logs --json | logdy
# Stream a single app and also keep output in your terminal
pm2 logs api --json | logdy --fallthrough
# Follow existing PM2 log files directly from disk
logdy follow ~/.pm2/logs/api-out.log ~/.pm2/logs/api-error.log --full-readLogdy runs locally on 127.0.0.1:8080 and keeps logs in memory (up to --max-message-count, default 100000). Add --no-analytics --no-updates (-n -u) to opt out of anonymous telemetry and update checks.
Want step-by-step UI column setup? Read our dedicated guides:
- PM2 Log Viewer: Browse Logs in a Web UI
- Combining PM2 Logs from Multiple Processes into a Single Stream
Frequently Asked Questions
How do I show only the last 100 lines of PM2 logs without streaming?
Run pm2 logs --lines 100 --nostream for all apps, or pm2 logs <app-name> --lines 100 --nostream for a specific process.
How do I view only PM2 error logs?
Pass --err: pm2 logs <app-name> --err --lines 100 (add --nostream if you want it to exit immediately).
Does pm2 flush restart my running applications?
No. pm2 flush empties the current log files managed by PM2 without restarting your processes.
Should I use pm2-logrotate or system logrotate?
pm2-logrotate is cross-platform and installs in one command (pm2 install pm2-logrotate). On Linux servers that already manage service logs via /etc/logrotate.d/, sudo pm2 logrotate -u <user> avoids running an extra Node.js module process.
