|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const df = require( './defaults' );
- const chalk = require( 'chalk' );
- const { Writable } = require( 'stream' );
-
- function checkLogLevel( { levels, level }, method ) {
- return levels[level] >= levels[method];
- }
-
- function parseParams( str = '' ) {
- return str
- .replace( /[()"']*/g, '' )
- .split( ',' )
- .map( s => s.trim() )
- .filter( s => s !== '' )
- .map( s => isNaN( +s ) ? s : +s );
- }
-
- function generateConfig( options ) {
-
- 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: 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 || {} ),
- stdout: options.stdout || process.stdout,
- stderr: options.stderr || options.stdout || process.stderr
- } );
-
- config.tokensKeys = Object.keys( config.tokens );
- config.defaultTokens = df.tokens;
-
- return config;
- }
-
- function generatePrefix( method, { tokens, defaultTokens, format: prefix, tokensKeys }, msg ) {
- tokensKeys
- .sort( ( a, b ) => b.length - a.length )
- .forEach( key => {
- const token = tokens[key];
- const re = new RegExp( `:${key}(\\([^)]*\\))?(\\.\\w+)*`, 'g' );
- prefix = prefix.replace( re, ( match, params ) => {
- let ret = token( { method, defaultTokens, params: parseParams( params ), tokens, msg } );
- match.replace( params, '' ).split( '.' ).slice( 1 ).forEach( decorator => {
- ret = chalk`{${decorator} ${ret}}`;
- } );
-
- return 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;
- }
-
- function selectOutputStream( method, { levels, stdout, stderr } ) {
- return levels[method] <= 2 ? stderr : stdout;
- }
-
- class FakeStream extends Writable {
- constructor() {
- super();
- this._last_message = '';
- }
-
- get last_msg() {
- return this._last_message.replace( /\n$/, '' );
- }
-
- _write( chunk, enc, cb ) {
- this._last_message = chunk.toString();
- cb();
- }
- }
-
- module.exports = {
- checkLogLevel,
- parseParams,
- generateConfig,
- generatePrefix,
- selectOutputStream,
- FakeStream
- };
|