console-stamp
Advanced tools
Comparing version 3.0.0-rc2.0 to 3.0.0-rc3.0
@@ -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); |
@@ -23,2 +23,2 @@ const date = require('../tokens/date.js'); | ||
groupCount: 0 | ||
}; | ||
}; |
@@ -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, | ||
}; | ||
}; |
{ | ||
"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", |
@@ -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` |
@@ -12,2 +12,2 @@ function padRight ( str, len = 0 ) { | ||
label | ||
}; | ||
}; |
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
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
15259
166
257
8