且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向所有控制台消息添加时间戳

更新时间:2023-12-06 17:06:40

原来,你可以覆盖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
...