向所有控制台消息添加时间戳 [英] Adding timestamps to all console messages

查看:212
本文介绍了向所有控制台消息添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完整的部署的,基于Express的项目,其中有许多console.log()和console.error()语句。
该项目使用永久运行,将stdout和stderr指向两个单独的文件。



这一切都很好,但现在我缺少时间戳 -



我可以在我的代码中进行某种搜索/替换,或者在每个文件中使用一些覆盖控制台的npm模块,但我不知道想要触摸每个模型/路由文件,除非我绝对必须。



有一种方法,也许是一个Express中间件,将允许我添加一个时间戳给每个

解决方案

原来,你可以覆盖app.js文件顶部的控制台函数,并使其在每个其他模块中生效。我得到混合结果,因为我的一个模块是分叉作为 child_process 。一旦我将该行复制到该文件的顶部,所有的工作。



为了记录,我安装了模块控制台戳记( npm install console-stamp --save ),并将此行添加到app.js和childProcess.js的顶部:

  //在日志消息前添加时间戳
require('console-stamp')(console,'[HH:MM:ss.l]');我现在的问题是:date

格式的连接记录器使用UTC格式,而不是我在其他控制台调用中使用的格式。这很容易通过注册我自己的时间格式(作为副作用,要求 dateformat 模块控制台邮票自带的,而不是安装另一个):

  //因为logger只返回一个UTC版本的日期,我自己的日期格式 - 使用控制台
的内部模块express.logger.format('mydate',function(){
var df = require('console-stamp / node_modules / dateformat') ;
return df(new Date(),'HH:MM:ss.l');
});
app.use(express.logger('[:mydate]:method:url:status:res [content-length] - :remote-addr - :response-time ms'));

现在,我的日志文件看起来有组织(更好,可分析):

  [15:09:47.746] staging服务器侦听端口3000 
[15:09:49.322]连接到数据库服务器xxxxx成功
[15:09:52.743] GET / product 200 - - 127.0.0.1 - 214 ms
[15:09:52.929] GET /stylesheets/bootstrap-cerulean.min.css 304 - - 127.0.0.1 - 8 ms
[15:09:52.935] GET /javascripts/vendor/require.js 304 - - 127.0.0.1 - 3 ms
[15:09:53.085] GET /javascripts/product.js 304 - - 127.0.0.1 - 2 ms
...


I have a complete, deployed, Express-based project, with many console.log() and console.error() statements throughout. The project runs using forever, directing the stdout and stderr to 2 separate files.

It all works quite well, but now I'm missing timestamps - to know exactly when errors occurred.

I can do some kind of search/replace throughout my code, or use some npm module that overrides console in each file, but I do not want to touch every model/route file, unless I absolutely have to.

Is there a way, perhaps an Express middleware, that would allow me to add a timestamp to every call made, or do I have to manually add it?

解决方案

It turns out, you can override the console functions at the top of the app.js file, and have it take effect in every other module. I got mixed results because one of my modules is forked as a child_process. Once I copied the line to the top of that file as well, all works.

For the record, I installed the module console-stamp (npm install console-stamp --save), and added this line to the top of app.js and childProcess.js:

// add timestamps in front of log messages
require('console-stamp')(console, '[HH:MM:ss.l]');

My problem now was that the :date format of the connect logger uses UTC format, rather than the one I'm using in the other console calls. That was easily fixed by registering my own time format (and as a side effect, requiring the dateformat module that console stamp comes with, rather than installing another one):

// since logger only returns a UTC version of date, I'm defining my own date format - using an internal module from console-stamp
express.logger.format('mydate', function() {
    var df = require('console-stamp/node_modules/dateformat');
    return df(new Date(), 'HH:MM:ss.l');
});
app.use(express.logger('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));

Now my log files look organized (and better yet, parseable):

[15:09:47.746] staging server listening on port 3000
[15:09:49.322] connected to database server xxxxx successfully
[15:09:52.743] GET /product 200 - - 127.0.0.1 - 214 ms
[15:09:52.929] GET /stylesheets/bootstrap-cerulean.min.css 304 - - 127.0.0.1 - 8 ms
[15:09:52.935] GET /javascripts/vendor/require.js 304 - - 127.0.0.1 - 3 ms
[15:09:53.085] GET /javascripts/product.js 304 - - 127.0.0.1 - 2 ms
...

这篇关于向所有控制台消息添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆