Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding timestamps to all console messages

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?

like image 524
Traveling Tech Guy Avatar asked Sep 15 '13 15:09

Traveling Tech Guy


People also ask

What is console timeStamp?

The console. timeStamp method adds a single marker to the browser's Performance tool (Firefox, Chrome). This lets you correlate a point in your code with the other events recorded in the timeline, such as layout and paint events.

How do I log time in node JS?

timeLog() method is an inbuilt function in Nodejs that is used to display the time for each execution. This function is proved to be effective when used in a loop. Parameters: This function accepts two or more parameters. Return Value: This method displays the time for the execution.

How do I add a timestamp to a PowerShell command?

After hitting enter the timestamp will be added at the end of every command that is being typed into the PowerShell console. In the example below we use the “ls” command (to list folder content, and yes, we know the theoretically correct way to do this in PowerShell is “Get-ChildItem” but HEY C’mon!

How to log a timestamp in console?

If you need that, just log the timestamp first: log.call (console, Date.now ()); log.apply (console, arguments); Not if it's the same app/process. Console is a global object so if you hijack one of its functions like this it will keep being hijacked for all files which share that global object. So this should/could be placed in the app.js file?

When should I use message timestamps?

We recommend  If you are watching system messages appear in real-time, it’s obvious what time those events have occurred. However, if you need to review messages that have been collected and archived in the internal logging buffer or on a syslog server, message timestamps become really important.

How important are message timestamps in Catalyst switches?

However, if you need to review messages that have been collected and archived in the internal logging buffer or on a syslog server, message timestamps become really important. By default, Catalyst switches add a simple uptime timestamp to logging messages.


2 Answers

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 ... 
like image 150
Traveling Tech Guy Avatar answered Sep 26 '22 21:09

Traveling Tech Guy


The module log-timestamp works for me.

npm install log-timestamp 

It's simple to use:

console.log('Before log-timestamp'); require('log-timestamp'); console.log('After log-timestamp'); 
Before log-timestamp [2012-08-23T20:08:32.000Z] After log-timestamp 
like image 20
Sunding Wei Avatar answered Sep 26 '22 21:09

Sunding Wei