Application Logs应用程序日志
Once an application is started with PM2 you can consult and manage logs easily.使用PM2启动应用程序后,您可以轻松查阅和管理日志。
Log files are located in the folder 日志文件位于文件夹$HOME/.pm2/logs
.$HOME/.pm2/logs
中。
Log Views日志视图
To display application’s log you can use the command 要显示应用程序的日志,可以使用命令pm2 logs
pm2 logs
❯ pm2 logs -h
Usage: logs [options] [id|name|namespace]
stream logs file. Default stream all logs
Options:
--json json log output
--format formated log output
--raw raw output
--err only shows error output
--out only shows standard output
--lines <n> output the last N lines, instead of the last 15 by default
--timestamp [format] add timestamps (default format YYYY-MM-DD-HH:mm:ss)
--nostream print logs without lauching the log stream
--highlight [value] highlights the given value
-h, --help output usage information
Some important commands:一些重要的命令:
# Display all apps logs in realtime实时显示所有应用程序日志
pm2 logs
# Display only `api` application logs仅显示`api`应用程序日志
pm2 logs api
# Display new logs in json在json中显示新日志
pm2 logs --json
# Display 1000 lines of api log file显示1000行api日志文件
pm2 logs big-api --lines 1000
You can also check logs with the CLI dashboard:您还可以使用CLI仪表板检查日志:
pm2 monit
Log size limit日志大小限制
The module pm2-logrotate automatically rotate and keep all the logs file using a limited space on disk.模块pm2-logrotate使用磁盘上的有限空间自动旋转并保留所有日志文件。
To install it:要安装它,请执行以下操作:
pm2 install pm2-logrotate
Read more about pm2-logrotate here阅读更多关于pm2-logrotate的信息
Flushing logs冲洗日志
This will empty the current application logs managed by PM2:这将清空PM2管理的当前应用程序日志:
pm2 flush
pm2 flush <api> # Clear the logs for the app with name/id matching <api>使用名称/id匹配的<api>清空应用程序的日志
Application log options应用程序日志选项
When starting an application you can specify many options on how启动应用程序时,您可以指定许多有关如何启动的选项
CLI
When running 运行pm2 start app.js [OPTIONS]
you can pass any of this options to the CLI:pm2 start app.js [OPTIONS]
时,您可以将以下任何选项传递给CLI:
-l --log [path] specify filepath to output both out and error logs
-o --output <path> specify out log file
-e --error <path> specify error log file
--time prefix logs with standard formated timestamp
--log-date-format <format> prefix logs with custom formated timestamp
--merge-logs when running mutiple process with same app name, do not split file by id
Auto prefixing logs with Date自动为日志添加日期前缀
To easily prefix logs of apps you can pass the option 要方便地为应用程序日志添加前缀,可以传递选项--time
:--time
:
$ pm2 start app.js --time
# Or a running app或者一个运行app
$ pm2 restart app --time
Configuration file配置文件
Via configuration file you can pass the options:通过配置文件,您可以传递选项:
error_file | (string) | ||
out_file | (string) | ||
pid_file | (string) | ||
merge_logs | boolean | true | true ,请避免在日志文件中添加进程id后缀 |
log_date_format | (string) | “YYYY-MM-DD HH:mm Z” |
Disabling log suffix禁用日志后缀
For app in cluster mode (node.js) only; If you want that all instances of a clustered process logs into the same file you can use the option 仅适用于集群模式(node.js)下的应用程序;如果希望集群进程的所有实例都登录到同一个文件中,可以使用选项--merge-logs
or merge_logs: true
--merge-logs
或merge_logs: true
Disable logging禁用日志记录
To disable all logs to be written in disk you can set the option 要禁用在磁盘中写入的所有日志,可以将out_file
and error_file
to /dev/null
out_file
和error_file
选项设置为/dev/null
module.exports = {
apps : [{
name: 'Business News Watcher',
script: 'app.js',
instances: 1,
out_file: "/dev/null",
error_file: "/dev/null"
cron_restart: '0 0 * * *'
[...]
}]
}
You can provide 您可以提供/dev/null
or NULL
as output of logs (not depending on the platform, they are harcoded string)./dev/null
或NULL
作为日志的输出(不取决于平台,它们是经过harcoded的字符串)。
Setting up a native logrotate设置本机日志文件
sudo pm2 logrotate -u user
This will write a basic logrotate configuration to 这将向/etc/logrotate.d/pm2-user
that will look like this:/etc/logrotate.d/pm2-user
写入一个基本的logrotate配置,如下所示:
/home/user/.pm2/pm2.log /home/user/.pm2/logs/*.log {
rotate 12
weekly
missingok
notifempty
compress
delaycompress
create 0640 user user
}