PM2 logs show the process name and status by default, but not when each line was written

PM2 is a process manager for Node.js applications — it keeps your app running, restarts it if it crashes, and collects its output into log files. By default, those log files contain only the message itself. To see when each log entry happened, you need to configure PM2 to include a timestamp format in the output.

The simplest way is to add a single line to your PM2 configuration file or to the command that starts your app. This tells PM2 to prepend a date and time to every line it logs, making it much easier to trace when errors or events occurred.

Key Takeaways

  • PM2 does not include timestamps in logs by default, only the process name and message text.
  • You add timestamps by setting the log_date_format option in your PM2 ecosystem file or startup command.
  • The format string uses standard date placeholders like YYYY for year, MM for month, and HH:mm:ss for time.
  • After you change the configuration, you must restart the process for the new format to take effect.
  • You can test the format by running a straightforward command that logs output and checking the log file when ready.

Add timestamps using an ecosystem file

An ecosystem file is a JavaScript or JSON file that PM2 reads to know how to start and manage your app. If you already have one, open it in a text editor. If not, create a new file called ecosystem.config.js in your project root.

Inside the file, add the log_date_format key to the app configuration. Here is what a minimal example looks like:

module.exports = {   apps: [{     name: "my-app",     script: "app.js",     log_date_format: "YYYY-MM-DD HH:mm:ss Z"   }] };

The format string YYYY-MM-DD HH:mm:ss Z produces output like 2024-01-15 14:23:45 +0000. You can adjust it to show only what you need — for example, HH:mm:ss for just the time, or YYYY-MM-DD for just the date. After you save the file, restart your app with pm2 start ecosystem.config.js.

Add timestamps to an already-running process

If your app is already running under PM2 and you do not have an ecosystem file, you can still add timestamps without rewriting your startup command. Stop the process first with pm2 stop app-name, then delete it from PM2's list with pm2 delete app-name.

Next, restart it with the timestamp format built into the command itself. Use the --log-date-format flag when you start it:

pm2 start app.js --name "my-app" --log-date-format "YYYY-MM-DD HH:mm:ss Z"

PM2 will now include timestamps in all new log lines. Existing log files are not retroactively updated — only new entries will have the timestamp prefix.

Common timestamp format options

PM2 uses the moment.js date library, so the format string follows moment.js conventions. Here are the placeholders you will encounter most often:

PlaceholderMeaningExample
YYYYFour-digit year2024
MMTwo-digit month01, 12
DDTwo-digit day05, 31
HHTwo-digit hour (24-hour)00, 23
mmTwo-digit minute00, 59
ssTwo-digit second00, 59
ZTimezone offset+0000, -0500

A format of YYYY-MM-DD HH:mm:ss is readable and works well for most purposes. If you need milliseconds for very fast-moving logs, add SSS: YYYY-MM-DD HH:mm:ss.SSS.

Check that timestamps are actually appearing

After you restart your process, open the log file to confirm the timestamps are there. PM2 stores logs in ~/.pm2/logs/ by default. The file name follows the pattern app-name-out.log for standard output and app-name-error.log for errors.

You can view the log in real time with pm2 logs app-name, or open the file directly with a text editor or the tail command:

tail -f ~/.pm2/logs/my-app-out.log

Each line should now start with the timestamp you configured. If you see no timestamps, double-check that you restarted the process after changing the configuration — PM2 does not reload settings while the app is running.

Rotate logs to keep files from growing too large

Once you have timestamps in your logs, you may notice the log files grow quickly, especially if your app logs frequently. PM2 can automatically rotate logs — archive old ones and start a new file — so no single log file becomes too large to open.

Add the max_size and max_file options to your ecosystem file to rotate logs when they reach a certain size:

module.exports = {   apps: [{     name: "my-app",     script: "app.js",     log_date_format: "YYYY-MM-DD HH:mm:ss Z",     max_size: "10M",     max_file: 5   }] };

This configuration keeps up to 5 log files, each no larger than 10 megabytes. When a file hits 10M, PM2 compresses it and starts a new one. Older archived files are deleted once you have 5 total. Adjust the size and count based on how much disk space you want to use.

Frequently Asked Questions

Do I need to change my app's code to add timestamps?

No. PM2 adds the timestamp to every line it captures from your app's output, regardless of what your code actually prints. You only need to change the PM2 configuration.

Why are my timestamps not showing up after I restarted?

PM2 does not reload configuration while a process is running. You must stop the process with pm2 stop, then start it again with pm2 start or pm2 restart. straightforward restarting the Node.js app itself is not enough.

Can I add timestamps to logs from an app that is already running?

Yes, but you must delete and restart it. Run pm2 delete app-name, then pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm:ss Z". New log entries will have timestamps; old ones will not.

What if I want a different timestamp format than the examples?

Use any combination of the moment.js placeholders — YYYY, MM, DD, HH, mm, ss, Z, and others. For example, DD/MM/YYYY HH:mm produces 15/01/2024 14:23. Test your format string by restarting the process and checking the first few log lines.

Do old log files get timestamps if I change the format?

No. Only new log entries written after the configuration change will include timestamps. Old log files remain unchanged. If you need timestamps on historical logs, you would have to manually edit them or use a separate tool.