Keywords: PM2 log management | Node.js deployment | Linux operations
Abstract: This technical paper provides an in-depth examination of PM2's default log storage mechanisms in Linux systems, detailing the directory structure and naming conventions within $HOME/.pm2/logs/. Building upon the accepted answer, it integrates supplementary techniques including real-time monitoring via pm2 monit, cluster mode configuration considerations, and essential command operations. Through systematic technical analysis, the paper offers developers comprehensive insights into PM2 log management best practices, enhancing Node.js application deployment and maintenance efficiency.
Architectural Analysis of PM2 Logging System
PM2, as a leading process manager for Node.js applications, features a logging system that embodies the efficiency and standardization of modern DevOps tools. Under default configuration, PM2 stores application log files in specific subdirectories within the user's home directory, a design that ensures centralized log management while preventing system directory pollution.
Detailed Default Log Storage Path
According to PM2's official design specifications, all application logs managed through PM2 are by default saved in the $HOME/.pm2/logs/ directory. The $HOME variable in this path points to the current user's home directory, typically formatted as /home/username in standard Linux Ubuntu systems. This design ensures isolation and security of log files in multi-user environments.
Log file naming follows a clear pattern: XXX-err.log and XXX-out.log, where XXX represents the application name registered in PM2. For instance, if an application is started with pm2 start app.js --name myapp, the system generates two files: myapp-err.log (error logs) and myapp-out.log (standard output logs). This separated storage design facilitates targeted analysis of different log levels.
Advanced Log Management Techniques
Beyond basic log file location, PM2 offers various advanced management tools. The pm2 monit command launches a terminal dashboard for real-time monitoring of all processes' CPU, memory usage, and log output. This console interface is particularly useful for rapid problem diagnosis in server environments, eliminating the need for frequent terminal switching or raw log file inspection.
In cluster deployment scenarios, PM2 supports launching application instances equal to the number of CPU cores via pm2 start myApp.js -i max. It's important to note that while official documentation often uses JavaScript configuration files as examples, JSON format configuration files generally offer better compatibility and stability in practice. Developers should pay special attention to configuration details in cluster mode to avoid deployment failures due to format issues.
Essential Command Operations
Efficient log management requires proficient command operations. Below is a set of core PM2 commands and their application scenarios:
pm2 list: View all running PM2 processes and their statuses, the foundational command for daily monitoringpm2 start all/pm2 stop all/pm2 delete all: Batch management of all application processes, improving operational efficiencypm2 logs [app-name]: Real-time viewing of specific application log output, supporting tail -f style streamingpm2 flush: Clear all log files, suitable for log rotation or disk space cleanup scenarios
Configuration Optimization Recommendations
For production environment deployments, developers are advised to customize log storage paths. By setting the error_file and out_file parameters in PM2's JSON configuration file, logs can be redirected to dedicated storage volumes or log management systems. For example:
{
"name": "myapp",
"script": "app.js",
"error_file": "/var/log/pm2/myapp-error.log",
"out_file": "/var/log/pm2/myapp-output.log",
"log_date_format": "YYYY-MM-DD HH:mm:ss"
}
This configuration approach not only enhances log management flexibility but also facilitates better integration with enterprise-level log collection and analysis platforms. Additionally, setting the log_date_format parameter standardizes log timestamp formats, simplifying subsequent log analysis and aggregation.
Security and Permission Considerations
In the default $HOME/.pm2/logs/ path, log files inherit the permission settings of the user directory. In multi-user server environments, this may lead to exposure of sensitive log information. The following security enhancements are recommended:
- Regularly review log file permissions to ensure only authorized users have access
- Utilize log rotation tools (such as logrotate) to manage log file sizes and historical retention
- Consider storing production environment logs on separate partitions to prevent log growth from affecting system performance
By deeply understanding PM2's log storage mechanisms and mastering related management techniques, developers can build more robust and maintainable Node.js application deployment systems. Whether for simple development testing or complex enterprise deployments, proper log management strategies are crucial factors in ensuring application stability.