node.js 如何将日期添加到 pm2 错误日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21317852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add dates to pm2 error logs?
提问by Plastic Rabbit
Is there a way to add timestamps to error logs in .pm2/logs?
有没有办法向 .pm2/logs 中的错误日志添加时间戳?
I noticed that pm2 logscommand shows aggregated logs with timestamps, but looking into log files - there are only messages and stacktraces without dates.
我注意到该pm2 logs命令显示带有时间戳的聚合日志,但查看日志文件 - 只有没有日期的消息和堆栈跟踪。


回答by AbdelHady
As per this issue& this commit, you can use:
pm2 start app.js --log-date-format 'DD-MM HH:mm:ss.SSS'
where 'DD-MM HH:mm:ss.SSS'is any momentjsvalid format.
'DD-MM HH:mm:ss.SSS'任何momentjs有效格式在哪里。
-- Update --
- 更新 -
As per the pm2 logs official documentation, now you can use the simpler --time, like:
根据pm2 日志官方文档,现在您可以使用更简单的--time,例如:
pm2 start app.js --time
where --timeprefixs logs with standard formatted timestamp.
其中--time带有标准格式时间戳的前缀日志。
回答by Raptor
As per the command line help (pm2 logs -h) running pm2 logs --timestampcommand should add the timestamp to the logs. However it does seem to not affect old logs! Apparently only new logs show up with timestamp.
根据命令行帮助 ( pm2 logs -h) 运行pm2 logs --timestamp命令应将时间戳添加到日志中。但是它似乎不会影响旧日志!显然只有新日志显示时间戳。
To fix this issue pass --log-date-format="YYYY-MM-DD HH:mm Z"to pm2 as a param. For example:
要解决此问题--log-date-format="YYYY-MM-DD HH:mm Z",请将其作为参数传递给 pm2。例如:
pm2 start bin/www --log-date-format="YYYY-MM-DD HH:mm Z"
Using process.json
使用 process.json
I like process.json for starting my app for convenience so my process.json contains the following:
为了方便起见,我喜欢 process.json 来启动我的应用程序,因此我的 process.json 包含以下内容:
{
"apps" : [
{
"name" : "app",
"script" : "bin/www",
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}
]
}
then I start my app by just running:
然后我通过运行来启动我的应用程序:
pm2 start process.json
Once done I see the timestamp showing up just by running:pm2 logsNotice that I didn't have to specify --timestamp to see the timestamp.
完成后,我会通过运行看到时间戳显示:pm2 logs请注意,我不必指定 --timestamp 来查看时间戳。
app (out): 2016-08-04 13:46 +01:00: My log here
A good read: http://pm2.keymetrics.io/docs/usage/log-management/
一个很好的阅读:http: //pm2.keymetrics.io/docs/usage/log-management/
回答by Randost
pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm"
回答by Michael
I use PM2, but I don't care for the logs that much. Instead I use bunyan, which gives a ton of flexibility for logging. If you npm install it with --globalyou can also use it as a live log viewer:
我使用 PM2,但我不太关心日志。相反,我使用 bunyan,它为日志记录提供了很大的灵活性。如果您使用--global 进行npm install 它,您还可以将其用作实时日志查看器:
This won't timestamp your console.log output, though. But If you convert to log.info()or any other Bunyan log function you will get nice logging.
但是,这不会为您的 console.log 输出添加时间戳。但是,如果您转换为log.info()或任何其他班扬日志功能,您将获得不错的日志记录。
To view live pm2 logs with bunyan, just pipe it:
要使用 bunyan 查看实时 pm2 日志,只需使用管道:
pm2 logs | bunyan
回答by Fraction
To use standard formated timestamp:
要使用标准格式的时间戳:
pm2 start app.js --time
Or if you want to prefix logs with custom formated timestamp:
或者,如果您想使用自定义格式的时间戳作为日志前缀:
pm2 start app.js --log-date-format <format>
Where <format>is a moment display format(eg YYYY-MM-DD HH:mm Z).
哪里<format>是矩显示格式(例如YYYY-MM-DD HH:mm Z)。
And if your app is already running you can use reloadfor a 0-second-downtimereload:
如果你的应用程序已经在运行,你可以使用reload一个0秒的停机时间重装:
pm2 reload app.js --time
Or
或者
pm2 reload app.js --log-date-format <format>
回答by mnhmilu
For process.yml , follow these example format. It Worked for me
对于 process.yml ,请遵循这些示例格式。它对我有用
apps:
- script : ./SampleApi/app.js
name : 'api-proxy-app'
instances: 2
exec_mode: cluster
watch : true
log_date_format : "YYYY-MM-DD HH:mm Z"
Sample Log format with DateTime:
带日期时间的示例日志格式:
2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for cancel mandate--####################################
2019-07-28 13:46 +06:00: channel created for exception scenario--####################################
2019-07-28 13:46 +06:00: channel created for create mandate--####################################
2019-07-28 13:46 +06:00: create channel initiated for cancel mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for create mandate--------------------->
2019-07-28 13:46 +06:00: create channel initiated for update mandate--------------------->
Using --log-date-format didn't worked for me.
使用 --log-date-format 对我不起作用。
回答by Manohar Reddy Poreddy
Wasted 30 mins on this.
浪费了30分钟。
- Din't work
- Other answers din't work
- Official CLIdin't work too:
pm2 start app.js [OPTIONS], ex:pm2 start app.js --time
- Worked
- Official Ecosystem fileworked great (procedure is below)
- 不工作
- 其他答案不起作用
- 官方 CLI 也不起作用:
pm2 start app.js [OPTIONS],例如:pm2 start app.js --time
- 工作过
- 官方生态系统文件运行良好(程序如下)
- Create an Ecosystem file
pm2-config.jsin your application root (ex: besidepackage.json)
pm2-config.js在您的应用程序根目录中创建一个生态系统文件(例如:besidepackage.json)
Paste the below contents & save:
粘贴以下内容并保存:
module.exports = {
apps: [
{
name: "my-app1",
script: "./index.js",
time: true, // <----------------------- This is the key to make it work
watch: false,
env: {
PORT: 4001,
NODE_ENV: "production",
},
},
],
};
- Now create a shell script
start.sh(OR, batch file, OR, directly run below commands)
- 现在创建一个shell脚本
start.sh(或,批处理文件,或,直接在命令下面运行)
Paste the below contents & save:
粘贴以下内容并保存:
pm2 stop pm2-config.js
pm2 delete pm2-config.js
pm2 start pm2-config.js

