console-stamp
Advanced tools
Comparing version 0.1.6 to 0.2.0
66
main.js
@@ -1,2 +0,2 @@ | ||
/*globals module:false, require:false, process:false*/ | ||
/*jshint node:true, bitwise:false */ | ||
/** | ||
@@ -6,23 +6,38 @@ * | ||
* | ||
* Inspired by https://github.com/FGRibreau/node-nice-console | ||
* | ||
* Dependencies: | ||
* dateFormat by felixge, is to be found here: https://github.com/felixge/node-dateformat | ||
* | ||
*/ | ||
"use strict"; | ||
var dateFormat = require( "dateformat" ); | ||
var merge = require( "merge" ); | ||
var colors = require( "colors" ); | ||
var defaults = require( "./defaults.json" ); | ||
var util = require( 'util' ); | ||
module.exports = function ( con, pattern, prefix_metadata ) { | ||
module.exports = function ( con, options, prefix_metadata ) { | ||
"use strict"; | ||
if ( con.__ts__ ) { | ||
return; | ||
// If the console is patched already, restore it | ||
if ( con.__ts__ && "restoreConsole" in con ) { | ||
con.restoreConsole(); | ||
} | ||
if ( typeof prefix_metadata === 'object' ) { | ||
var util = require('util'); | ||
var pattern; | ||
if ( typeof options === "string" ) { | ||
// Fallback to version 0.1.x | ||
pattern = options; | ||
options = merge( {}, defaults ); | ||
} else { | ||
options = merge( {}, defaults, (options || {}) ); | ||
pattern = options.pattern; | ||
prefix_metadata = prefix_metadata || options.metadata; | ||
} | ||
options.include = options.include.filter( function filter( m ) { | ||
return !~options.exclude.indexOf( m ); | ||
} ); | ||
// Set the color theme | ||
colors.setTheme( options.colors ); | ||
var original_functions = []; | ||
@@ -32,5 +47,5 @@ | ||
['log', 'info', 'warn', 'error', 'dir', 'assert'].forEach( function ( f ) { | ||
options.include.forEach( function ( f ) { | ||
original_functions.push( [ f, con[f] ] ); | ||
original_functions.push( [f, con[f]] ); | ||
@@ -41,13 +56,24 @@ var org = con[f]; | ||
var prefix = "[" + dateFormat( pattern ) + "] [" + f.toUpperCase() + "] ", | ||
args = slice.call( arguments ); | ||
var prefix = ("[" + dateFormat( pattern ) + "]").stamp + " "; | ||
var args = slice.call( arguments ); | ||
// Add label if flag is set | ||
if ( options.label ) { | ||
prefix += ("[" + f.toUpperCase() + "]").label + " ".substr( f.length ); | ||
} | ||
// Add metadata if any | ||
var metadata = ""; | ||
if ( typeof prefix_metadata === 'function' ) { | ||
prefix = prefix + prefix_metadata( f, args ) + ' '; | ||
metadata = prefix_metadata( f, args ); | ||
} else if ( typeof prefix_metadata === 'object' ) { | ||
prefix = prefix + util.inspect( prefix_metadata ) + ' '; | ||
metadata = util.inspect( prefix_metadata ); | ||
} else if ( typeof prefix_metadata !== 'undefined' ) { | ||
prefix = prefix + prefix_metadata + ' '; | ||
metadata = prefix_metadata; | ||
} | ||
if ( metadata ) { | ||
prefix += metadata.metadata + " "; | ||
} | ||
if ( f === "error" || f === "warn" || ( f === "assert" && !args[0] ) ) { | ||
@@ -54,0 +80,0 @@ process.stderr.write( prefix ); |
{ | ||
"name": "console-stamp", | ||
"main": "main", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -10,3 +10,3 @@ "name": "Ståle Raknes", | ||
}, | ||
"contributors":[ | ||
"contributors": [ | ||
{ | ||
@@ -24,3 +24,2 @@ "name": "Ståle Raknes", | ||
} | ||
], | ||
@@ -41,4 +40,9 @@ "description": "Patch NodeJS console methods in order to add timestamp information by pattern", | ||
"dependencies": { | ||
"dateformat": "1.0.7-1.2.3" | ||
"dateformat": "^1.0.11", | ||
"merge": "^1.2.0", | ||
"colors": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"filesize": "^3.1.2" | ||
} | ||
} |
149
README.md
# console-stamp | ||
Patch Node.js console methods in order to add timestamp information by pattern. | ||
[![npm][npm-image]][npm-url] | ||
[![downloads][downloads-image]][downloads-url] | ||
[npm-image]: https://img.shields.io/npm/v/console-stamp.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/console-stamp | ||
[downloads-image]: https://img.shields.io/npm/dm/console-stamp.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/console-stamp | ||
This module enables you to patch the console's methods in Node.js, to add timestamp prefix based on a given string pattern, and more... | ||
## Usage ## | ||
### Installing ### | ||
### Install | ||
npm install console-stamp | ||
### Patching the console ### | ||
### Patching the console | ||
require("console-stamp")(console, [options]); | ||
#### console | ||
The console itself. | ||
#### options {Object|String} | ||
From version 2.0 the second parameter is an object with several options. As a backward compatibillity feature this parameter can be a string containing the pattern. | ||
* **options.pattern** {String}<br>A string with date format based on [Javascript Date Format](http://blog.stevenlevithan.com/archives/date-time-format)<br>**Default**: "ddd mmm dd yyyy HH:MM:ss" | ||
* **options.label** {Boolean}<br>If true it will show the label (LOG | INFO | WARN | ERROR)<br>**Default**: true | ||
* **options.include** {Array}<br>An array containing the methods to include in the patch<br>**Default**: ["log", "info", "warn", "error", "dir", "assert"] | ||
* **options.exclude** {Array}<br>An array containing the methods to exclude in the patch<br>**Default**: [] \(none) | ||
* **options.metadata** {String/Object/Function}<br>Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples.<br>**Note** that metadata can still be sent as the third parameter (as in vesion 1.6) as a backward compatibillity feature, but this is deprecated. <br>**Default**: undefined | ||
* **options.colors** {Object}<br>An object representing a color theme. More info [here](https://www.npmjs.com/package/colors). | ||
* **options.colors.stamp** {String/Array} <br>**Default:** [] | ||
* **options.colors.label** {String/Array} <br>**Default:** [] | ||
* **options.colors.metadata** {String/Array} <br>**Default:** [] | ||
Note: To combine colors, bgColors and style, set them as an array like this: | ||
... | ||
stamp: ["black", "bgYellow", "underline"] | ||
... | ||
Note also that by sending the parameter `--no-color` when you start your node app, will prevent any colors from console. | ||
$ node my-app.js --no-color | ||
### Example | ||
// Patch console.x methods in order to add timestamp information | ||
require("console-stamp")(console, "HH:MM:ss.l"); | ||
require( "console-stamp" )( console, { pattern : "dd/mm/yyyy HH:MM:ss.l" } ); | ||
console.log("Hello World!"); | ||
// -> [14:02:48.062] [LOG] Hello World! | ||
// -> [26/06/2015 14:02:48.062] [LOG] Hello World! | ||
var port = 8080; | ||
console.log("Server running at port %d", port); | ||
// -> [16:02:35.325] [LOG] Server running at port 8080 | ||
// -> [26/06/2015 16:02:35.325] [LOG] 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" ); | ||
console.log( "This is a console.log message" ); | ||
console.info( "This is a console.info message" ); | ||
console.warn( "This is a console.warn message" ); | ||
console.error( "This is a console.error message" ); | ||
console.dir( {bar: "This is a console.dir message"} ); | ||
Result: | ||
[20:04:05.969] [LOG] LOG | ||
[20:04:05.972] [INFO] INFO | ||
[20:04:05.972] [WARN] WARN | ||
[20:04:05.972] [ERROR] ERROR | ||
[20:04:05.972] [DIR] { bar: 'console.dir' } | ||
[20:04:05.975] [ERROR] 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:906:3 | ||
[20:04:05.975] [LOG] MyTimer: 6ms | ||
[20:04:05.976] [ASSERT] | ||
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:906:3 | ||
[26/06/2015 12:44:31.777] [LOG] This is a console.log message | ||
[26/06/2015 12:44:31.777] [INFO] This is a console.info message | ||
[26/06/2015 12:44:31.779] [WARN] This is a console.warn message | ||
[26/06/2015 12:44:31.779] [ERROR] This is a console.error message | ||
[26/06/2015 12:44:31.779] [DIR] { bar: 'This is a console.dir message' } | ||
See more about timestamp patterns at [felixges][felixge] excellent [dateformat][dateformat] | ||
and | ||
[dateformat]: https://github.com/felixge/node-dateformat | ||
[felixge]: https://github.com/felixge | ||
[FGRibreau]: https://github.com/FGRibreau/node-nice-console | ||
require( "console-stamp" )( console, { | ||
metadata: function () { | ||
return ("[" + process.memoryUsage().rss + "]"); | ||
}, | ||
colors: { | ||
stamp: "yellow", | ||
label: "white", | ||
metadata: "green" | ||
} | ||
} ); | ||
console.log( "This is a console.log message" ); | ||
console.info( "This is a console.info message" ); | ||
console.warn( "This is a console.warn message" ); | ||
console.error( "This is a console.error message" ); | ||
console.dir( {bar: "This is a console.dir message"} ); | ||
Result: | ||
![Console](gfx/console.png) | ||
### Adding Metadata ### | ||
Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples. | ||
Types can be string, object (interpreted with util.inspect), or function. | ||
See the [test-metadata.js](https://github.com/starak/node-console-stamp/blob/master/test-metadata.js) for examples. | ||
### String example | ||
#### String example | ||
require("console-stamp")(console, "HH:MM:ss.l", '[' + process.pid + ']'); | ||
require("console-stamp")(console, { | ||
pattern:"HH:MM:ss.l", | ||
metadata:'[' + process.pid + ']' | ||
}); | ||
@@ -83,9 +124,11 @@ console.log('Metadata applied.'); | ||
[18:10:30.875] [LOG] [7785] Metadata applied. | ||
[26/06/2015 12:44:31.779] [LOG] [7785] Metadata applied. | ||
### Function example | ||
#### Function example | ||
var util = require("util"); | ||
require("console-stamp")(console, "HH:MM:ss.l", function(){ return '[' + (process.memoryUsage().rss) + ']'; }); | ||
require("console-stamp")(console, { | ||
pattern:"HH:MM:ss.l", | ||
metadata: function(){ return '[' + (process.memoryUsage().rss) + ']'; }); | ||
@@ -92,0 +135,0 @@ console.log('Metadata applied.'); |
@@ -28,5 +28,5 @@ #!/usr/bin/env node | ||
if ( typeof metadata !== 'undefined' ) { | ||
require("./main")(console, format, metadata); | ||
require("./main")(console, {pattern:format, metadata:metadata}); | ||
} else { | ||
require("./main")(console, format); | ||
require("./main")(console, {pattern:format}); | ||
} | ||
@@ -51,3 +51,3 @@ console.log('Metadata applied'); | ||
var test = function(){ | ||
require("./main")(console, "HH:MM:ss.l", showMemoryUsage); | ||
require("./main")(console, {metadata:showMemoryUsage}); | ||
console.info("Reading 50MB..."); | ||
@@ -54,0 +54,0 @@ // Read in 10MB |
108
test.js
@@ -1,17 +0,99 @@ | ||
/*globals console:false, require:false*/ | ||
/*jshint node:true*/ | ||
"use strict"; | ||
require( "./main" )( console, "HH:MM:ss.l" ); | ||
var human_readable = require( "filesize" ); | ||
var write = function ( txt ) { | ||
process.stdout.write( txt + "\n" ); | ||
}; | ||
var console_stamp = require( "./main" ); | ||
var foo = { bar: "console.dir" }, | ||
count = 11; | ||
function run() { | ||
console.log( "This is a console.log message" ); | ||
console.info( "This is a console.info message" ); | ||
console.warn( "This is a console.warn message" ); | ||
console.error( "This is a console.error message" ); | ||
console.dir( {bar: "This is a console.dir message"} ); | ||
write( " " ); | ||
} | ||
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" ); | ||
write( "No patching" ); | ||
run(); | ||
write( "Patched with defaults" ); | ||
console_stamp( console ); | ||
run(); | ||
write( "Patched with 1.x pattern as string and some metadata" ); | ||
console_stamp( console, "dd/mm/yyyy HH:MM:ss.l", "METADATA" ); | ||
run(); | ||
write( "Patched with new 2.x object literal options parameter, with pattern" ); | ||
console_stamp( console, { | ||
pattern: "dd/mm/yyyy HH:MM:ss.l" | ||
} ); | ||
run(); | ||
write( "Patched with defaults, but no label" ); | ||
console_stamp( console, { | ||
label: false | ||
} ); | ||
run(); | ||
write( "Patched with defaults and metadata" ); | ||
console_stamp( console, { | ||
metadata: "[" + process.pid + "]" | ||
} ); | ||
run(); | ||
write( "Patched with include parameter set to: ['log', 'info']" ); | ||
console_stamp( console, { | ||
include: ["log", "info"] | ||
} ); | ||
run(); | ||
write( "Patched with exclude parameter set to: ['log', 'info']" ); | ||
console_stamp( console, { | ||
exclude: ["log", "info"] | ||
} ); | ||
run(); | ||
write( "Patched with advanced metadata and color theme" ); | ||
console_stamp( console, { | ||
metadata: function () { | ||
return ("[" + human_readable( process.memoryUsage().rss ) + "]"); | ||
}, | ||
colors: { | ||
stamp: ["yellow"], | ||
label: ["white"], | ||
metadata: ["green"] | ||
} | ||
} ); | ||
run(); | ||
// Use some memory | ||
var size = 5000000; | ||
write( "Reading " + human_readable( size ) + "..." ); | ||
var fs = require( "fs" ); | ||
var Buffer = require( 'buffer' ).Buffer; | ||
var buffer = new Buffer( size ); | ||
var fd = fs.openSync( '/dev/urandom', 'r' ); | ||
fs.readSync( fd, buffer, 0, buffer.length, 0 ); | ||
fs.closeSync( fd ); | ||
write( "Finished reading (memory usage should be different)." ); | ||
write( "Patched with advanced metadata and color theme" ); | ||
console_stamp( console, { | ||
metadata: function () { | ||
return ("[" + human_readable( process.memoryUsage().rss ) + "]"); | ||
}, | ||
colors: { | ||
stamp: "blue", | ||
label: ["white"], | ||
metadata: "green" | ||
} | ||
} ); | ||
run(); | ||
console_stamp( console ); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
95199
23
230
139
3
1
5
+ Addedcolors@^1.1.2
+ Addedmerge@^1.2.0
+ Addedarray-find-index@1.0.2(transitive)
+ Addedcamelcase@2.1.1(transitive)
+ Addedcamelcase-keys@2.1.0(transitive)
+ Addedcolors@1.4.0(transitive)
+ Addedcurrently-unhandled@0.4.1(transitive)
+ Addeddateformat@1.0.12(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-stdin@4.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedindent-string@2.1.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-finite@1.1.0(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedloud-rejection@1.6.0(transitive)
+ Addedmap-obj@1.0.1(transitive)
+ Addedmeow@3.7.0(transitive)
+ Addedmerge@1.2.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedredent@1.0.0(transitive)
+ Addedrepeating@2.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedstrip-indent@1.0.1(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtrim-newlines@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
- Removeddateformat@1.0.7-1.2.3(transitive)
Updateddateformat@^1.0.11