Software⏱️ 3 min read📅 2026-06-04

How to Fix: How to add dates to pm2 error logs?

Add timestamps to pm2 error logs by enabling the timestamp option in your pm2.json configuration file.

Quick Answer: Enable the timestamp option in your pm2.json configuration file, e.g. "logDate: true"

The issue of adding dates to pm2 error logs can be frustrating when troubleshooting applications. This problem affects users who rely on log files for debugging and analysis purposes.

Adding timestamps to error logs is essential for tracking errors, identifying patterns, and optimizing application performance. In this guide, we will walk you through the steps to add dates to pm2 error logs.

⚠️ Common Causes

  • The primary reason for this issue is that pm2 logs do not automatically include timestamps by default. This is because pm2 uses a logging format that prioritizes log content over timestamp information.
  • An alternative cause could be that the logging configuration is not properly set up to include timestamps.

🔧 Proven Troubleshooting Steps

Configuring pm2 to Include Timestamps

  1. Step 1: To add timestamps to pm2 error logs, you need to configure the logging format in your pm2.json file. Open the pm2.json file and update the logging configuration as follows: `logDate: true`.
  2. Step 2: This will include a timestamp in each log entry. You can also customize the log format by adding additional options, such as log level or log content.
  3. Step 3: Save the changes to your pm2.json file and restart pm2 for the new configuration to take effect.

Using a Third-Party Logger

  1. Step 1: If you are unable to modify the pm2 logging configuration, you can use a third-party logger like Winston or Morgan to add timestamps to your logs.
  2. Step 2: Install the required package using npm or yarn: `npm install winston` or `yarn add winston`.
  3. Step 3: Configure the logger in your application code as follows: `const logger = require('winston'); const logFormat = winston.format.combine(winston.format.timestamp(), winston.format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`)); logger.add(new winston.transports.Console({ format: logFormat }));`.
  4. Step 4: This will add a timestamp to each log entry. You can customize the logging format as needed.

💡 Conclusion

In conclusion, adding dates to pm2 error logs is a straightforward process that requires configuring the logging format or using a third-party logger. By following these steps, you can improve your ability to track errors and optimize application performance.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions