WHERE PM2 LOGS ARE STORED

WHERE PM2 LOGS ARE STORED

PM2 is a process manager for Node.js applications that helps monitor and maintain them. Its logging capabilities allow you to track the performance and behavior of your apps. Understanding where PM2 logs are stored is crucial for troubleshooting issues, debugging errors, and gaining insights into your applications' operation.

Log File Locations

  1. Default Location:

    • Unix-based Systems: /var/log/pm2/
    • Windows Systems: %USERPROFILE%\AppData\Local\pm2\logs\
  2. User-Defined Location:

    • PM2 allows you to specify a custom log directory using the --log-dir flag when starting your application.
    • The logs will be stored in the specified directory.
  3. Log File Naming Convention:

    • PM2 uses the following naming convention for its log files:
      • pm2-<app_name>-out.log: Contains standard output (stdout) logs.
      • pm2-<app_name>-error.log: Contains standard error (stderr) logs.
      • pm2-<app_name>.log: Contains combined stdout and stderr logs.

Accessing Log Files

  1. Local Access:

    • On Unix-based systems, you can use the command sudo cat /var/log/pm2/<app_name>*.log to view the logs.
    • On Windows systems, you can open the log files from the %USERPROFILE%\AppData\Local\pm2\logs\ directory.
  2. Remote Access:

    • If you are running PM2 on a remote server, you can use SSH to connect to the server and access the log files as mentioned above.

Log Rotation

PM2 automatically rotates log files when they reach a certain size or age. By default, the maximum log file size is 10 MB, and the maximum age is one day.

Customizing Log Settings

  1. Changing Log Level:

    • You can adjust the log level by setting the --log-level flag when starting your application.
    • Possible log levels include error, warn, info, and debug.
  2. Changing Log Format:

    • PM2 supports various log formats, including JSON, pretty-print, and raw.
    • You can specify the desired format using the --log-type flag.
  3. Enabling/Disabling Timestamps:

    • You can control whether timestamps are included in the logs using the --log-timestamp flag.
  BJJ WHERE TO WATCH

Conclusion

Understanding where PM2 logs are stored and how to access them is essential for monitoring and maintaining your Node.js applications. By leveraging PM2's logging capabilities, you can gain valuable insights into the performance, behavior, and errors of your apps, enabling you to troubleshoot issues and ensure smooth operation.

FAQs

  1. Can I change the default log file location?

    • Yes, you can specify a custom log directory using the --log-dir flag when starting your application.
  2. How can I view PM2 logs remotely?

    • You can use SSH to connect to the remote server and access the log files as mentioned in the article.
  3. How do I change the log level?

    • You can adjust the log level by setting the --log-level flag when starting your application.
  4. Can I change the log format?

    • Yes, PM2 supports various log formats, which you can specify using the --log-type flag.
  5. How can I enable or disable timestamps in the logs?

    • You can use the --log-timestamp flag to control whether timestamps are included in the logs.

Christophe McLaughlin

Website:

Leave a Reply

Your email address will not be published. Required fields are marked *

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box