【问题标题】:How to put datetime in logfile with forever node如何使用永久节点将日期时间放入日志文件中
【发布时间】:2019-10-26 09:48:15
【问题描述】:

我需要知道请求是在什么时候提出来衡量时间的。

到处搜索,但我没有找到将日期添加到日志的方法。

这是日志显示请求的方式

GET /api/user/getSessionsByDate 200 253.230 ms - -
POST /api/schedule/getDate 200 162.697 ms - -

我需要显示如下所示的日期时间

2019-01-01T15:03:01 GET /api/user/getSessionsByDate 200 253.230 ms - -
2019-01-01T15:03:01 POST /api/schedule/getDate 200 162.697 ms - -

我需要这样做才能在日志存档中获得每个请求的时间。 例如,在此日志中,我需要在文件“distribution.log”的每一行中包含时间

  uid                forever pid   id      logfile                                 

distribution-prod   10428   11531    /distr/.forever/distribution.log

试试下面的命令还是不行:

forever set timestamp true

【问题讨论】:

  • 我在路由器上获取时间没有问题,我需要做的是让每个请求都在日志存档中进行。例如,在此日志中,我需要在文件“distribution.log”的每一行中包含时间 uid forever pid id logfile distribution-prod 10428 11531 /root/.forever/distribution.log

标签: node.js forever


【解决方案1】:

我找到了一个解决方案,为了使用日期时间记录请求 https,我使用了 morgan(node.js 的 HTTP 请求记录器中间件)。

通过下一个配置,我们可以得到日期时间和其他数据。

import logger from 'morgan';

logger.format('customformat', '[:date[iso]] ":method :url" :status :res[content-length] - :response-time ms');

app.use(logger('customformat'));

我希望它对某人有用

非常感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-19
    • 2021-03-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    相关资源
    最近更新 更多