ログ

アプリケーションログ

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 [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 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'(デフォルトではrawログを記録します)

ログサフィックスの無効化

クラスターモード(node.js)のアプリの場合のみ。クラスター化されたプロセスのすべてのインスタンスが同じファイルにログを記録する場合は、オプション--merge-logsまたはmerge_logs:trueを使用できます。

ロギングの無効化

ディスクへのすべてのログ書き込みを無効にするには、オプションout_fileerror_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
}
このページに貢献する