Skip to content

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 ​

bash
# 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 flush

By 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]

FlagWhat It DoesExample
--lines <n>Output the last N lines instead of the default 15pm2 logs api --lines 200
--nostreamPrint the requested lines and exit without tailingpm2 logs api --lines 100 --nostream
--errShow only standard error (stderr) logspm2 logs api --err
--outShow only standard output (stdout) logspm2 logs api --out
--jsonOutput live log entries as JSON objectspm2 logs --json
--formatFormatted log outputpm2 logs --format
--rawRaw output without PM2 prefixespm2 logs api --raw
--timestamp [format]Add timestamps (default YYYY-MM-DD-HH:mm:ss)pm2 logs --timestamp
--highlight [value]Highlight matching text in the outputpm2 logs --highlight ERROR

Tip for scripting & grep: Always add --nostream when piping pm2 logs into grep, tail, or CI checks—otherwise the command waits indefinitely for new log lines:

bash
pm2 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:

json
{
  "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:

text
$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:

bash
# 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-logs

Flushing and Reloading PM2 Logs ​

To empty current application log files managed by PM2:

bash
# Flush logs for all apps
pm2 flush

# Flush logs for a single app (by name or ID)
pm2 flush api

If an external tool (like Linux logrotate) rotates files out from under PM2, tell PM2 to reopen its log file descriptors:

bash
pm2 reloadLogs

Automatic 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):

bash
pm2 install pm2-logrotate

Inspect active settings with pm2 conf, and adjust them with pm2 set pm2-logrotate:<key> <value>:

bash
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 * * *'
SettingDefaultDescription
max_size10MRotate when a log file exceeds this size (K, M, G)
retain30Number of rotated archive files to keep (in addition to active file)
compressfalseCompress rotated logs with gzip
dateFormatYYYY-MM-DD_HH-mm-ssTimestamp format appended to rotated filenames
rotateModuletrueAlso rotate logs of PM2 modules
workerInterval30How often (in seconds, min 1) the worker checks file sizes
rotateInterval0 0 * * *Cron schedule forcing rotation (default: daily at midnight)
TZSystem timeStandard 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:

bash
sudo pm2 logrotate -u youruser

Browse 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:

bash
# 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-read

Logdy 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:


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.