Super light-weight stand-in replacement for console logging to only output JSON
npm install json-console-logger --save
import logger from 'json-console-logger';
logger.log('a string');
// => console.log('{"level":"LOG","message":"a string"}');
logger.log('a string', 'another string');
// => console.log('{"level":"LOG","message":["a string","another string"]}');
logger.info('a string', { an: 'object' });
// => console.info('{"level":"INFO","message":["a string",{"an":"object"}]}');
logger.warn('a string', ['an', 'array']);
// => console.warn('{"level":"WARN","message":["a string",["an","array"]]}');
logger.error('a string', new Error('an error'));
// => console.error('{"level":"ERROR","message":["a string",{"error":"Error","message":"an error","stack":"..."}]}');
import logger from 'json-console-logger';
logger.on('error', (message) => {
// do something
});
logger.error('an error string');
// => console.error('{"level":"ERROR","message":"an error string"}');
// => callback will be called with '{"level":"ERROR","message":"an error string"}' as argument
logger.error('a string', new Error('an error'));
// => console.error('{"level":"ERROR","message":["a string",{"error":"Error","message":"an error","stack":"..."}]}');
// => callback will be called with '{"level":"ERROR","message":["a string",{"error":"Error","message":"an error","stack":"..."}]}' as argument
import logger from 'json-console-logger';
logger.setConfiguration({ log: false, info: false });
logger.log('a string');
// => logs nothing
logger.info('a string');
// => logs nothing
logger.warn('an warn string');
// => console.error('{"level":"WARN","message":"an warn string"}');
logger.error('an error string');
// => console.error('{"level":"ERROR","message":"an error string"}');
import logger from 'json-console-logger';
logger.setConfiguration({ logger: (msg) => process.stdout.write(`${msg}\n`) });
logger.log('a string');
// => process.stdout.write('{"level":"LOG","message":"a string"}\n');
MIT