應用程式日誌
一旦應用程式使用 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
用法:日誌 [選項] [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 的更多資訊 here
清除日誌
這將清空 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/<app name>-error-<pid>.log) | |
out_file | (字串) | 輸出檔案路徑(預設為 $HOME/.pm2/logs/<app name>-out-<pid>.log) | |
log_file | (字串) | 輸出和錯誤日誌的檔案路徑(預設停用) | |
pid_file | (字串) | pid 檔案路徑(預設為 $HOME/.pm2/pids/<app name>-<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
}