console.log 消息未显示在 pm2 日志文件中

问题描述 投票:0回答:1

我做了一个简单的测试脚本:

function tester() {
  console.log('hello there', process.env.NODE_APP_INSTANCE);
  setTimeout(tester, 1000);
}
tester();

我的 pm2 配置文件是:

module.exports = {
  apps: [
    {
      name: 'tester',
      script: 'node scripts/tester',
    }
  ]
}

我开始pm2:

npx pm2 start config.cjs --name tester -i 5 --time

我明白了:

[PM2] Applying action restartProcessId on app [tester](ids: [ 0, 1, 2, 3, 4 ])
[PM2] [tester](0) ✓
[PM2] [tester](1) ✓
[PM2] [tester](2) ✓
[PM2] [tester](3) ✓
[PM2] [tester](4) ✓
[PM2] Process successfully started
┌────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ tester    │ default     │ 1.0.0   │ cluster │ 73218    │ 0s     │ 0    │ online    │ 0%       │ 52.0mb   │ patrick  │ disabled │
│ 1  │ tester    │ default     │ 1.0.0   │ cluster │ 73219    │ 0s     │ 0    │ online    │ 0%       │ 52.3mb   │ patrick  │ disabled │
│ 2  │ tester    │ default     │ 1.0.0   │ cluster │ 73220    │ 0s     │ 0    │ online    │ 0%       │ 49.6mb   │ patrick  │ disabled │
│ 3  │ tester    │ default     │ 1.0.0   │ cluster │ 73223    │ 0s     │ 0    │ online    │ 0%       │ 49.6mb   │ patrick  │ disabled │
│ 4  │ tester    │ default     │ 1.0.0   │ cluster │ 73226    │ 0s     │ 0    │ online    │ 0%       │ 46.7mb   │ patrick  │ disabled │
└────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

pm2 日志显示:

PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:0] starting in -cluster mode-
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:1] starting in -cluster mode-
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:0] online
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:1] online
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:2] starting in -cluster mode-
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:3] starting in -cluster mode-
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:2] online
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:4] starting in -cluster mode-
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:3] online
PM2        | 2024-09-23T22:32:29: PM2 log: App [tester:4] online

/Users/patrick/.pm2/logs/tester-out-0.log last 15 lines:
/Users/patrick/.pm2/logs/tester-error-0.log last 15 lines:
/Users/patrick/.pm2/logs/tester-out-1.log last 15 lines:
/Users/patrick/.pm2/logs/tester-error-1.log last 15 lines:
/Users/patrick/.pm2/logs/tester-out-2.log last 15 lines:
/Users/patrick/.pm2/logs/tester-error-2.log last 15 lines:
/Users/patrick/.pm2/logs/tester-out-3.log last 15 lines:
/Users/patrick/.pm2/logs/tester-error-3.log last 15 lines:
/Users/patrick/.pm2/logs/tester-out-4.log last 15 lines:
/Users/patrick/.pm2/logs/tester-error-4.log last 15 lines:

我的日志目录有:

> ls -l .pm2/logs/
total 0
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-error-0.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-error-1.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-error-2.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-error-3.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-error-4.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:05 tester-error.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-out-0.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-out-1.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-out-2.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-out-3.log
-rw-r--r--  1 patrick  staff  0 Sep 23 22:24 tester-out-4.log
-rw-r--r--  1 patrick  staff  0 Sep 23 20:45 tester-out.log

为什么控制台日志语句没有写入日志文件?

next.js
1个回答
0
投票

看来这是配置文件名的问题。我已经能够使用您提供的文件重现您的问题,并将

config.cjs
更改为
tester.config.js
有效。

这是文档中的相关行:

如果您要创建自己的配置文件,请确保它以 .config.js 结尾,以便 PM2 能够将其识别为配置文件。

通过上述更改(

config.cjs
->
tester.config.js
),我能够让日志正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.