应用程序日志
使用 PM2 启动应用程序后,您可以轻松地查看和管理日志。
日志文件位于 $HOME/.pm2/logs
文件夹中。
日志视图
要显示应用程序的日志,您可以使用命令 pm2 logs
-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 formatted timestamp
--log-date-format <format> prefix logs with custom formatted timestamp
--merge-logs when running multiple process with same app name, do not split file by id
用法: logs [选项] [id | 名称 | 命名空间] |
流式传输日志文件。默认情况下流式传输所有日志
选项
--json json log output
--format formatted 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 launching the log stream
--highlight [value] highlights the given value
-h, --help output usage information ```
一些重要的命令
# Display all apps logs in realtime
pm2 logs
# Display only `api` application logs
pm2 logs api
# Display new logs in json
pm2 logs --json
# Display 1000 lines of api log file
pm2 logs big-api --lines 1000
您也可以使用 CLI 仪表板查看日志
pm2 monit
对于应用程序的每一行,将打印以下元数据
{
"message": "echo\n", // the actual message that has been `console.log`
"timestamp": "2017-02-06T14:51:38.896Z", // timestamp of the message, can be formatted
"type": "out", // the type of logs, can be `err`, `out` or `PM2`
"process_id": 0, // the process id used by PM2
"app_name": "one-echo" // the application name
}
模块 pm2-logrotate 会自动轮转并保留所有日志文件,并在磁盘上使用有限的空间。
要安装它
pm2 install pm2-logrotate
阅读有关 pm2-logrotate 的更多信息,请访问 此处
刷新日志
这将清空 PM2 管理的当前应用程序日志
pm2 flush
pm2 flush <api> # Clear the logs for the app with name/id matching <api>
应用程序日志选项
启动应用程序时,您可以指定许多关于如何
CLI
运行 pm2 start app.js [选项]
时,您可以将任何这些选项传递给 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 formatted timestamp
--log-date-format <format> prefix logs with custom formatted timestamp
--merge-logs when running multiple process with same app name, do not split file by id
使用日期自动添加日志前缀
要轻松地为应用程序的日志添加前缀,您可以传递选项 --time
$ pm2 start app.js --time
# Or a running app
$ pm2 restart app --time
配置文件
您可以通过配置文件传递以下选项
字段 | 类型 | 示例 | 描述 |
---|---|---|---|
error_file | (字符串) | 错误文件路径(默认为 $HOME/.pm2/logs/<应用程序名称>-error-<pid>.log) | |
out_file | (字符串) | 输出文件路径(默认为 $HOME/.pm2/logs/<应用程序名称>-out-<pid>.log) | |
log_file | (字符串) | 输出和错误日志的文件路径(默认情况下禁用) | |
pid_file | (字符串) | pid 文件路径(默认为 $HOME/.pm2/pids/<应用程序名称>-<pid>.pid) | |
merge_logs | 布尔值 | true | 如果设置为 true,则避免在日志文件名后缀添加进程 ID |
log_date_format | (字符串) | “YYYY-MM-DD HH:mm Z” | 日志日期格式(请参阅日志部分) |
log_type | (字符串) | “json” | 指定日志输出样式,可能的值:'json'(默认情况下将记录原始日志) |
禁用日志后缀
仅适用于集群模式下的应用程序(node.js);如果您希望集群进程的所有实例都记录到同一个文件中,则可以使用选项 --merge-logs
或 merge_logs: true
禁用日志记录
要禁用将所有日志写入磁盘,可以将选项 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 * * *'
[...]
}]
}
您可以提供 /dev/null
或 NULL
作为日志输出(不依赖于平台,它们是硬编码字符串)。
设置原生 logrotate
sudo pm2 logrotate -u user
这会将一个基本的 logrotate 配置写入 /etc/logrotate.d/pm2-user
,如下所示
/home/user/.pm2/pm2.log /home/user/.pm2/logs/*.log {
rotate 12
weekly
missingok
notifempty
compress
delaycompress
create 0640 user user
}