Package Exports
- console-stamp
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (console-stamp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Console-stamp 3.0.0 RC3
This module enables you to patch the console's methods in Node.js, to add prefix based on tokens, and more...
Usage
Install
npm install console-stamp@nextSimple example
This simple example is using the default settings
require( 'console-stamp' )( console );
console.log('Hello, World!');
// -> [10.02.2019 15:37:43.452] [LOG] Hello, World!Patching the console
require( 'console-stamp' )( console, [options] );console
The global console or custom console.
options {Object|String}
The second parameter is an object with several options. As a feature this parameter can be a string containing the date-format.
options.format {String}
A string with date format based on dateformat
Default: 'dd.mm.yyyy HH:MM:ss.l'options.tokens {Object}
Containing token-functions. See example here.options.include {Array}
An array containing the methods to include in the patch
Default: ["debug", "log", "info", "warn", "error"]options.level {String}
A string choosing the most verbose logging function to allow.
Default:logoptions.extend {Object}
An object describing methods and their associated log level, to extend the existingmethod <-> log levelpairs.
For an example see Custom methods.options.stdout {WritableStream}
A customstdoutto use with custom console.
Default:process.stdoutoptions.stderr {WritableStream}
A customstderrto use with custom console.
Default:options.stdoutorprocess.stderr
Tokens
There are only two predefined tokens registered by default. These are:
:date([format][,utc])[.color]
:label([padding])[.color]:date([format][,utc])
- format {String}
Containing the date format based on dateformat
Default: 'dd.mm.yyyy HH:MM:ss.l' - utc {Boolean}
Set totruewill return UTC-time
Default: false
:label([padding])
- padding {Number}
The total length of the label, including the brackets and padding
Default: 7
TODO: How to write custom tokens
Create a custom token
To define your own token, simply add a callback function with the token name to the tokens option. This callback function is expected to return a string. The value returned is then available as ":foo()" in this case:
require( 'console-stamp' )( console, {
format: ':foo() :label(7)',
tokens:{
foo: () => {
return '[my prefix]';
}
}
} );
console.log("Bar")
// > [my prefix] [LOG] BarThe token callback function is called with one argument, representing an Object with the following properties:
method{String}
The invoked methodparams{Array}
The token parameters (ex: The token call:label(7)will have params[7])tokens{Object}
All the defined tokens, incl. the defaultsdefaultTokens{Object}
Only the default tokens, even if it's been redefined in options
Example
Here we are making a custom date token called mydate using moment.js to format the date
const moment = require('moment');
moment.locale('ja');
require( 'console-stamp' )( console, {
format: ':mydate() :label(7)',
tokens:{
mydate: () => {
return `[${moment().format('LLLL')}]`;
}
}
} );
console.log('This is a console.log message');
console.info('This is a console.info message');
console.debug('This is a console.debug message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');Result:
[2016年5月12日午前11時10分 木曜日] [LOG] This is a console.log message
[2016年5月12日午前11時10分 木曜日] [INFO] This is a console.info message
[2016年5月12日午前11時10分 木曜日] [DEBUG] This is a console.debug message
[2016年5月12日午前11時10分 木曜日] [WARN] This is a console.warn message
[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error messageColors and styling
All tokens can have a trailing styling like this:
require( 'console-stamp' )( console, {
format: ':foo().blue.bgWhite.underline :label(7)',
tokens:{
foo: () => {
return 'bar';
}
}
} );TODO: Color Groups
ex: (foo).yellow
Note that by sending the parameter --no-color when you start your node app, will prevent any colors from console.
$ node my-app.js --no-colorExample
// Patch console.x methods in order to add timestamp information
require( 'console-stamp' )( console, { format: ':date(dd/mm/yyyy HH:MM:ss.l) :label' } );
console.log('Hello World!');
// -> [26/06/2015 14:02:48.062] [LOG] Hello World!
const port = 8080;
console.log('Server running at port %d', port);
// -> [26/06/2015 16:02:35.325] [LOG] Server running at port 8080
console.log('This is a console.log message');
console.info('This is a console.info message');
console.debug('This is a console.debug message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');Result:
[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.778] [DEBUG] 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
Custom Console
You can also create a custom console with its own stdout and stderr like this:
const fs = require('fs');
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
const logger = new console.Console(output, errorOutput);
require('console-stamp')(logger, {
stdout: output,
stderr: errorOutput
});Everything is then written to the files.
NOTE: If stderr isn't passed, warning and error output will be sent to the given stdout.
Custom Methods
The option.extend option enables the extension or modification of the logging methods and their associated log levels:
The default logging methods and their log levels are as follows:
levels: {
error: 1,
warn: 2,
info: 3,
log: 4,
debug: 4
};The extend option enables the usage of custom console logging methods to be used with this module, for example:
// Extending the console with a custom method
console.fatal = function(msg) {
console.org.error(msg);
process.exit(1);
}
// Initialising the output formatter
require( 'console-stamp' )( console, {
extend: {
fatal: 1
}
} );Note how the console.org.error method used in the custom method. This is to prevent circular calls to console.error