console-stamp
Advanced tools
+26
| const console_stamp = require( './index' ); | ||
| const chalk = require( "chalk" ); | ||
| const { padRight } = require('./lib/utils'); | ||
| const map = { | ||
| error: 'red', | ||
| info: 'blue', | ||
| warn: 'yellow' | ||
| }; | ||
| const logger = new console.Console( process.stdout ); | ||
| console_stamp( console, { | ||
| format: '(->).yellow :date(HH:MM.ss.l).gray :label(9)', | ||
| tokens: { | ||
| label: ( obj ) => { | ||
| //logger.error(obj); | ||
| return chalk`{${map[obj.method] || 'reset'} ${obj.defaultTokens.label( obj )}}`; | ||
| } | ||
| } | ||
| } ); | ||
| console.log( 'Log msg' ); | ||
| console.info( 'Info msg' ); | ||
| console.warn( 'Warn msg' ); | ||
| console.error( 'Error msg' ); |
+7
-1
@@ -0,1 +1,7 @@ | ||
| /* | ||
| * node-console-stamp | ||
| * (c) 2013-2019 StΓ₯le Lytskjold Raknes <https://github.com/starak/node-console-stamp> | ||
| * MIT license | ||
| */ | ||
| const { checkLogLevel, generateConfig, generatePrefix, selectOutputStream } = require( './lib/utils.js' ); | ||
@@ -9,3 +15,3 @@ | ||
| // Fix lack of debug alias in pre 8.0 node | ||
| // Fix the lack of debug alias in pre 8.0 node | ||
| if(typeof con.debug === "undefined"){ | ||
@@ -12,0 +18,0 @@ con.debug = (...arg) => con.org.log ? con.org.log(...arg) : con.log(...arg); |
+1
-1
@@ -23,2 +23,2 @@ const date = require('../tokens/date.js'); | ||
| groupCount: 0 | ||
| }; | ||
| }; |
+17
-7
@@ -9,3 +9,2 @@ const df = require( './defaults.js' ); | ||
| function parseParams( str = '' ) { | ||
| // noinspection JSCheckFunctionSignatures | ||
| return str | ||
@@ -16,3 +15,3 @@ .replace( /[()"']*/g, '' ) | ||
| .filter( s => s !== '' ) | ||
| .map( s => isNaN( s ) ? s : +s ); | ||
| .map( s => isNaN( +s ) ? s : +s ); | ||
| } | ||
@@ -32,4 +31,4 @@ | ||
| include: [...( new Set( [...( options.include || df.include ), ...Object.keys( options.extend || {} )] ) )], | ||
| tokens: Object.assign( df.tokens, options.tokens || {} ), | ||
| levels: Object.assign( df.levels, options.levels || {}, options.extend || {} ), | ||
| tokens: Object.assign( {}, df.tokens, options.tokens || {} ), | ||
| levels: Object.assign( {}, df.levels, options.levels || {}, options.extend || {} ), | ||
| stdout: options.stdout || process.stdout, | ||
@@ -50,5 +49,5 @@ stderr: options.stderr || options.stdout || process.stderr | ||
| const token = tokens[key]; | ||
| const re = new RegExp( `:${key}(\\([^)]*\\))?(\\.\\w+)*`, 'g' ); | ||
| const re = new RegExp( `:${key}(\\([^)]+\\))?(\\.\\w+)*`, 'g' ); | ||
| prefix = prefix.replace( re, ( match, params ) => { | ||
| let ret = token( { method, defaultTokens, params: parseParams( params ) } ); | ||
| let ret = token( { method, defaultTokens, params: parseParams( params ), tokens } ); | ||
| match.replace( params, '' ).split( '.' ).slice( 1 ).forEach( decorator => { | ||
@@ -60,3 +59,14 @@ ret = chalk`{${decorator} ${ret}}`; | ||
| } ); | ||
| } ); | ||
| // Color groups | ||
| const rec = /(\([^)]*\))(\.\w+)+/g; | ||
| prefix = prefix.replace( rec, ( match, text ) => { | ||
| let ret = text.replace( /[\(\)]/g, '' ); | ||
| match.replace( text, '' ).split( '.' ).slice( 1 ).forEach( decorator => { | ||
| ret = chalk`{${decorator} ${ret}}`; | ||
| } ); | ||
| return ret; | ||
| } ) | ||
| return prefix; | ||
@@ -75,2 +85,2 @@ } | ||
| selectOutputStream, | ||
| }; | ||
| }; |
+1
-1
| { | ||
| "name": "console-stamp", | ||
| "main": "index.js", | ||
| "version": "3.0.0-rc2.0", | ||
| "version": "3.0.0-rc3.0", | ||
| "scripts": { | ||
@@ -6,0 +6,0 @@ "test": "tap -R spec test/*.js", |
+34
-4
@@ -1,2 +0,2 @@ | ||
| # Console-stamp 3.0.0 RC2 | ||
| # Console-stamp 3.0.0 RC3 | ||
@@ -69,3 +69,3 @@ [![npm][npm-image]][npm-url] | ||
| There are only two tokens registered by default: | ||
| There are only two predefined tokens registered by default. These are: | ||
@@ -89,5 +89,32 @@ :date([format][,utc])[.color] | ||
| ### **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: | ||
| ```javascript | ||
| require( 'console-stamp' )( console, { | ||
| format: ':foo() :label(7)', | ||
| tokens:{ | ||
| foo: () => { | ||
| return '[my prefix]'; | ||
| } | ||
| } | ||
| } ); | ||
| console.log("Bar") | ||
| // > [my prefix] [LOG] Bar | ||
| ``` | ||
| The token callback function is called with one argument, representing an Object with the following properties: | ||
| * `method` {String} <br> | ||
| The invoked method | ||
| * `params` {Array} <br> | ||
| The token parameters (ex: The token call `:label(7)` will have params `[7]`) | ||
| * `tokens` {Object} <br> | ||
| All the defined tokens, incl. the defaults | ||
| * `defaultTokens` {Object} <br> | ||
| Only the default tokens, even if it's been redefined in options | ||
| #### Example | ||
| Making a custom date token using moment.js to format the date | ||
| Here we are making a custom date token called `mydate` using moment.js to format the date | ||
| ```js | ||
@@ -137,2 +164,5 @@ const moment = require('moment'); | ||
| ### **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. | ||
@@ -229,2 +259,2 @@ ```console | ||
| **Note** how the `console.org.error` method used in the custom method. This is to prevent circular calls to log | ||
| **Note** how the `console.org.error` method used in the custom method. This is to prevent circular calls to `console.error` |
+1
-1
@@ -12,2 +12,2 @@ function padRight ( str, len = 0 ) { | ||
| label | ||
| }; | ||
| }; |
-15
| console.fatal = ( ...args ) => { | ||
| ( console.org || console ).error( ...args ); | ||
| process.exit( 1 ); | ||
| } | ||
| require( '.' )( console, { | ||
| include: [ | ||
| 'log' | ||
| ], | ||
| extend: { | ||
| fatal: 1 | ||
| } | ||
| } ); | ||
| console.fatal( 'Test' ); |
-20
| const moment = require('moment'); | ||
| require('./index')(console, { | ||
| format: ':mydate(ja) :label(7)', | ||
| tokens:{ | ||
| mydate: ({ params: [locale] }) => { | ||
| moment.locale(locale); | ||
| 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'); | ||
| console.reset(); | ||
| console.debug('This is a console.debug message'); |
15259
9.28%166
6.41%257
13.22%8
-11.11%