diff --git a/README.md b/README.md index e51f2c9..73750f1 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ From version 2.0 the second parameter is an object with several options. As a ba * **options.level** {String}
A string choosing the most verbose logging function to allow. Ordered/grouped as such: "log dir", "info", "warn assert", "error"
**Default**: log +* **options.extend** {Object}
An object describing methods and their associated log level, to extend the existing `method <-> log level` pairs.
For an example see [Custom methods](#custommethods). + * **options.metadata** {String/Object/Function}
Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples.
**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.
**Default**: undefined * **options.stdout** {WritableStream}
A custom `stdout` to use with [custom console](#customconsole).
**Default:** `process.stdout` @@ -135,7 +137,7 @@ As of version 0.2.4 you can also create a custom console with its own `stdout` a var output = fs.createWriteStream( './stdout.log' ); var errorOutput = fs.createWriteStream( './stderr.log' ); var logger = new console.Console( output, errorOutput ); - + console_stamp( logger, { stdout: output, stderr: errorOutput @@ -149,7 +151,7 @@ Everything is then written to the files. ### Custom Formatter Example -Custom forrmatter using moment.js +Custom formatter using moment.js var moment = require('moment'); moment.locale('ja'); @@ -174,6 +176,50 @@ Result: [2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message [2016年5月12日午前11時10分 木曜日] [DIR] { bar: 'This is a console.dir message' } + +### Custom Methods + +The **option.extend** option enables the extention or modification of the logging methods and their associated log levels: + +The default logging methods and their log levels are as follows: + +```javascript +var levelPriorities = { + log: 4, + info: 3, + warn: 2, + error: 1, + assert: 2, + dir: 4 +}; +``` + +Combined with the **include** option, the **extend** option enables the usage of custom console logging methods to be used with this module, for example: + +```javascript +// Extending the console object with custom methods +console.debug = function(msg) { + console.log(msg); +} +console.fatal = function(msg) { + console.log(msg); + process.exit(1); +} + +// Initialising the output formatter +require('console-stamp')(console, { + pattern: "HH:MM:ss", + extend: { + debug: 5, + fatal: 0, + }, + include: ["debug", "info", "warn", "error", "fatal"], + level: "debug", +}); +``` + +**Note** how the `log` method is omitted from the `include` list. Because the custom functions call `console.log` internally, including the `log` method would print double-formatted output. + ### Adding Metadata ### Types can be string, object (interpreted with util.inspect), or function. diff --git a/defaults.json b/defaults.json index a9dc8b2..87cca63 100644 --- a/defaults.json +++ b/defaults.json @@ -4,6 +4,7 @@ "exclude": [], "disable": [], "level": "log", + "extend": {}, "label": true, "colors": { "stamp":[], diff --git a/main.js b/main.js index 7f66068..0ab7180 100644 --- a/main.js +++ b/main.js @@ -13,31 +13,6 @@ var chalk = require( "chalk" ); var defaults = require( "./defaults.json" ); var util = require( 'util' ); -var levelPriorities = { - log: 4, - info: 3, - warn: 2, - error: 1, - assert: 2, - dir: 4 -}; - -function getAllowedLogFunctions( level ) { - var logFunctions = [], - levelPriority = levelPriorities[level]; - - for ( var logFunction in levelPriorities ) { - if ( levelPriorities.hasOwnProperty( logFunction ) ) { - if ( levelPriority >= levelPriorities[logFunction] ) { - logFunctions.push( logFunction ); - } - } - - } - - return logFunctions; -} - module.exports = function ( con, options, prefix_metadata ) { // If the console is patched already, restore it @@ -60,6 +35,32 @@ module.exports = function ( con, options, prefix_metadata ) { var stdout = options.stdout; var stderr = options.stderr || options.stdout; + var levelPriorities = { + log: 4, + info: 3, + warn: 2, + error: 1, + assert: 2, + dir: 4 + }; + + //Extend log levels + levelPriorities = merge( {}, levelPriorities, (options.extend || {}) ); + + var getAllowedLogFunctions = function ( level ) { + var logFunctions = [], + levelPriority = levelPriorities[level]; + + for ( var logFunction in levelPriorities ) { + if ( levelPriorities.hasOwnProperty( logFunction ) ) { + if ( levelPriority >= levelPriorities[logFunction] ) { + logFunctions.push( logFunction ); + } + } + } + return logFunctions; + }; + var dateFormat = options.formatter || defaultDateFormat, allowedLogFunctions = getAllowedLogFunctions( options.level ); diff --git a/package.json b/package.json index 3d1bab7..e23fc12 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,10 @@ { "name": "Steffan Donal", "url": "https://github.com/SteffanDonal" + }, + { + "name": "Sören Schwert", + "url": "https://github.com/sisou" } ], "description": "Patch NodeJS console methods in order to add timestamp information by pattern",