Skip to content

Commit

Permalink
Handling console.table, done todos in README.md, upgrading dependenci…
Browse files Browse the repository at this point in the history
…es and some minor tweaks here and there.
  • Loading branch information
starak committed Jan 7, 2021
1 parent 242d381 commit 2beac58
Show file tree
Hide file tree
Showing 7 changed files with 2,390 additions and 2,391 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ require( 'console-stamp' )( console, {
format: ':date().blue.bgWhite.underline :label(7)'
} );
```
You can also simply place some text in parenthesis, and then add your styling to that.

### **TODO: Color Groups**
ex: `(foo).yellow`
```js
require( 'console-stamp' )( console, {
format: '(->).yellow :date().blue.bgWhite.underline :label(7)'
} );
```

**Note** that by sending the parameter `--no-color` when you start your node app, will prevent any colors from console.
```console
Expand All @@ -153,12 +157,11 @@ For more examples on styling, check out the [chalk](https://www.npmjs.com/packag
<a name="tokens"></a>
### Tokens

TODO: Add the `:msg` token

There are only two predefined tokens registered by default. These are:
There are only three predefined tokens registered by default. These are:

:date([format][,utc])[.color]
:label([padding])[.color]
:msg[.color]

**:date([format][,utc])**
* **format** {String}<br>
Expand All @@ -173,6 +176,8 @@ There are only two predefined tokens registered by default. These are:
The total length of the label, including the brackets and padding<br>
**Default:** 7

**:msg**
* If the `:msg` token is provided in `format`, the output from the console will be returned in its place, otherwise the console output will be added as the last output, with no formatting.

#### 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:
Expand All @@ -196,6 +201,8 @@ console.log("Bar")
The token callback function is called with one argument, representing an Object with the following properties:
* `method` {String} <br>
The invoked method
* `msg` {String} <br>
The console output as a string
* `params` {Array} <br>
The token parameters (ex: The token call `:label(7)` will have params `[7]`)
* `tokens` {Object} <br>
Expand Down Expand Up @@ -226,7 +233,7 @@ console.error('This is a console.error message');
```

Result:
```console
```terminal
[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
Expand All @@ -243,7 +250,7 @@ The **option.extend** option enables the extension or modification of the loggin
The default logging methods and their log levels are as follows:

```js
levels: {
levels = {
error: 1,
warn: 2,
info: 3,
Expand Down Expand Up @@ -308,5 +315,5 @@ The second parameter is an object with several options. As a feature this parame
* **options.stderr** {WritableStream}<br>A custom `stderr` to use with [custom console](#customconsole).<br>
**Default:** `options.stdout` or `process.stderr`

* **options.use_custom_message** TODO!
* **options.preventDefaultMessage** {Boolean}<br>If set to `true` Console-stamp will not print out the standard output from the console. This can be used in combination with a custom message token.<br>**Default:** `false`

13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = consoleStamp = ( con, options = {} ) => {
const config = generateConfig( options );
const include = config.include.filter( m => typeof con[m] === 'function' );

const helperConsole = new console.Console( config.stdout, config.stderr );
const org = {};
Object.keys( con ).forEach( m => org[m] = con[m] );
con.org = org;
Expand All @@ -30,8 +31,12 @@ module.exports = consoleStamp = ( con, options = {} ) => {
if ( checkLogLevel( config, method ) ) {
customConsole.log.apply( context, arguments );
stream.write( `${generatePrefix( method, config, customConsoleStream.last_msg )} ` );
if ( config.use_custom_message || /\:msg\b/.test( config.format ) ) {
if ( config.preventDefaultMessage || /:msg\b/.test( config.format ) ) {
stream.write('\n');
}else if(method === 'table'){
stream.write('\n');
// Normaly table calls log to write to stream, so we need to prevent double prefix
helperConsole.table.apply( context, arguments);
}else if( !isCustom && options.stdout){
stream.write(`${customConsoleStream.last_msg}\n`);
} else {
Expand All @@ -44,11 +49,17 @@ module.exports = consoleStamp = ( con, options = {} ) => {
con.__patched = true
} );

if(!include.includes('table')) {
// Normaly table calls log to write to stream, we need to prevent prefix when table is not included
con.table = helperConsole.table;
}

con.reset = () => {
Object.keys( con.org ).forEach( m => {
con[m] = con.org[m];
delete con.org[m];
} );
delete con.org;
delete con.__patched;
delete con.reset;
customConsoleStream.end();
Expand Down
8 changes: 4 additions & 4 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module.exports = {
format: '',
dfFormat: ':date($$) :label(7)',
dfDateFormat: 'dd.mm.yyyy HH:MM.ss.l',
include: ["debug", "log", "info", "warn", "error"],
include: ['debug', 'log', 'info', 'warn', 'error'],
tokens: {
date,
label,
msg
},
level: "log",
level: 'log',
levels: {
error: 1,
warn: 2,
Expand All @@ -22,5 +22,5 @@ module.exports = {
},
extend: {},
groupCount: 0,
use_custom_message: false,
};
preventDefaultMessage: false,
};
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ function parseParams( str = '' ) {

function generateConfig( options ) {

if ( typeof options === "string" ) {
if ( typeof options === 'string' ) {
options = {
format: df.dfFormat.replace( '$$', options )
}
}

// Using Object.assign and not Object spread properties to support Node v6.4
const config = Object.assign( {}, df, options || {}, {
format: options.format || df.dfFormat.replace( '$$', df.dfDateFormat ),
format: typeof options.format === 'undefined' ? df.dfFormat.replace( '$$', df.dfDateFormat ) : options.format,
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 || {} ),
Expand Down
Loading

0 comments on commit 2beac58

Please sign in to comment.