Skip to content

Super light-weight stand-in replacement for console logging to only output JSON"

License

Notifications You must be signed in to change notification settings

sealninja/json-console-logger

Repository files navigation

JSON Console Logger

Super light-weight stand-in replacement for console logging to only output JSON

NPM version Downloads

Installation

npm install json-console-logger --save

Usage

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":"..."}]}');

Callbacks

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

Disable log levels

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"}');

Other output than console.log

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');

License

MIT

About

Super light-weight stand-in replacement for console logging to only output JSON"

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •