A configurable, lightweight, opinionated JavaScript logger.
Defined log levels are trace, debug, info, warn and error.
npm i js-console-logger --save
import 'js-console-logger';
Usage is very similar to console
object.
NOTE: consoleLevel is set to trace
.
logger.trace('Network', 'API is not reposding', { url: 'www.google.com', ua: 'bot' });
logger.debug('Network', 'API is not reposding', { url: 'www.google.com', ua: 'bot' });
logger.info('Network', 'API is not reposding', { url: 'www.google.com', ua: 'bot' });
logger.warn('Network', 'API is not reposding', { url: 'www.google.com', ua: 'bot' });
logger.error('Network', 'API is not reposding', { url: 'www.google.com', ua: 'bot' });
Default configuration is good enough to start. But you can configure few options
import { consoleConfig } from 'js-console-logger';
// import { LogLevel } from 'js-console-logger/lib/types';
const isProd = process.env.NODE_ENV === 'production';
consoleConfig.setDisableConsole(isProd); // default false
consoleConfig.setConsoleLevel('info');
// default info, Use types in TS.
consoleConfig.setDisableReport(!isProd); //
consoleConfig.setReportFn((obj) => {
// report it to somewhere, call api etc.
});
consoleConfig.setReportLevel('error');
// default error, Use types in TS.
import { consoleConfig } from 'js-console-logger';
import store from '../utils/store';
import { reportLog } from '../containers/App/actions';
const reportFn = (obj) => {
store.dispatch(reportLog(obj));
};
consoleConfig.setReportFn(reportFn);