Permalink
Browse files

New way of doing things. Using process.stdout to write out the stamp …

…before executing the original console. Introduced a simple test/example.
  • Loading branch information...
1 parent d44291f commit bc836f6cc642ef1dc9b8817d3394133a746f0c90 Ståle Raknes committed Feb 28, 2014
Showing with 76 additions and 14 deletions.
  1. +1 −0 .gitignore
  2. +42 −2 README.md
  3. +14 −10 main.js
  4. +2 −2 package.json
  5. +17 −0 test.js
View
@@ -1 +1,2 @@
+.idea
node_modules/
View
@@ -2,8 +2,6 @@
Patch Node.js console methods in order to add timestamp information by pattern.
-Inspired by [FGRibreau/node-nice-console][FGRibreau]
-
## Usage ##
### Installing ###
@@ -21,6 +19,48 @@ Inspired by [FGRibreau/node-nice-console][FGRibreau]
var port = 8080;
console.log("Server running at port %d", port);
// -> 16:02:35.325 Server running at port 8080
+
+### Example
+
+ console.time( "MyTimer" );
+ console.log( "LOG" );
+ console.info( "INFO" );
+ console.warn( "WARN" );
+ console.error( "ERROR" );
+ console.dir( { foo: "bar" } );
+ console.trace();
+ console.timeEnd( "MyTimer" );
+ console.assert( count < 10, "Count is > 10" );
+
+Result:
+
+ 20:01:27.500 LOG
+ 20:01:27.504 INFO
+ 20:01:27.504 WARN
+ 20:01:27.504 ERROR
+ 20:01:27.504 { bar: 'console.dir' }
+ 20:01:27.508 Trace
+ at Object.<anonymous> (/Users/starak/code/node-console-stamp/test.js:14:9)
+ at Module._compile (module.js:456:26)
+ at Object.Module._extensions..js (module.js:474:10)
+ at Module.load (module.js:356:32)
+ at Function.Module._load (module.js:312:12)
+ at Function.Module.runMain (module.js:497:10)
+ at startup (node.js:119:16)
+ at node.js:901:3
+ 20:01:27.510 MyTimer: 10ms
+ 20:01:27.510
+ AssertionError: Count is > 10
+ at Console.assert (console.js:102:23)
+ at Console.con.(anonymous function) [as assert] (/Users/starak/code/node-console-stamp/main.js:35:24)
+ at Object.<anonymous> (/Users/starak/code/node-console-stamp/test.js:16:9)
+ at Module._compile (module.js:456:26)
+ at Object.Module._extensions..js (module.js:474:10)
+ at Module.load (module.js:356:32)
+ at Function.Module._load (module.js:312:12)
+ at Function.Module.runMain (module.js:497:10)
+ at startup (node.js:119:16)
+ at node.js:901:3
See more about timestamp patterns at [felixges][felixge] excellent [dateformat][dateformat]
View
@@ -1,3 +1,4 @@
+/*globals module:false, require:false, process:false*/
/**
*
* Node Console stamp by Ståle Raknes
@@ -11,27 +12,30 @@
var dateFormat = require( "dateformat" );
-module.exports = function( con, pattern ) {
+module.exports = function ( con, pattern ) {
- if( con.__ts__ ){ return; }
+ "use strict";
+
+ if ( con.__ts__ ) {
+ return;
+ }
var slice = Array.prototype.slice;
- [ 'log', 'debug', 'info', 'warn', 'error' ].forEach( function(f){
+ [ 'log', 'info', 'warn', 'error', 'dir', 'assert' ].forEach( function ( f ) {
- var org = con[f];
+ var org = con[ f ];
- con[f] = function(){
+ con[f] = function () {
- var date = dateFormat( pattern ),
+ var date = dateFormat( pattern ) + " ",
args = slice.call( arguments );
- typeof args[0] === "string" ? args[0] = date + " " + args[0] : args.unshift( date );
-
- return org.apply(con, args);
+ process.stdout.write( date );
+ return org.apply( con, args );
};
- });
+ } );
con.__ts__ = true;
View
@@ -1,7 +1,7 @@
{
"name": "console-stamp",
"main": "main",
- "version": "0.1.1",
+ "version": "0.1.2",
"author": {
"name": "Ståle Raknes",
"email": "stale@raknes.net",
@@ -20,6 +20,6 @@
},
"license": "MIT",
"dependencies" : {
- "dateformat" : "*"
+ "dateformat" : "1.0.7-1.2.3"
}
}
View
@@ -0,0 +1,17 @@
+/*globals console:false, require:false*/
+
+require( "./main" )( console, "HH:MM:ss.l" );
+
+var foo = { bar: "console.dir" },
+ count = 11;
+
+console.time( "MyTimer" );
+console.log( "LOG" );
+console.info( "INFO" );
+console.warn( "WARN" );
+console.error( "ERROR" );
+console.dir( foo );
+console.trace();
+console.timeEnd( "MyTimer" );
+console.assert( count < 10, "Count is > 10" );
+

0 comments on commit bc836f6

Please sign in to comment.