console-logger demo

by plantface

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment-with-locales.min.js"></script>
<script src="https://cdn.rawgit.com/alexei/sprintf.js/1.0.2/dist/sprintf.min.js"></script>
<script src="https://rawgit.com/better-js-logging/console-logger/master/dist/console-logger.js"></script>

JavaScript

// old style
console.info('Hello Legacy %s!', 'world', { 'extra': ['pass-through params'] });

// new style
consoleLogger.prefixPattern = '%s::[%s]>';
consoleLogger.datetimePattern = 'LLL';
consoleLogger.logLevels = {
    '*': consoleLogger.LEVEL.OFF,
    'main': consoleLogger.LEVEL.WARN,
    'main.subB': consoleLogger.LEVEL.TRACE
};

consoleLogger.enhanceLogging(console);
console.log('-------------------------------'); // ignored, global context is mapped to '*'

consoleLogger.logLevels['*'] = consoleLogger.LEVEL.INFO;
console.log('-------------------------------');

console.getLogger('banana').debug('Hello brave new world!'); // ignored, logging set to INFO for '*'
console.getLogger('main.subA').info('Hello brave new world!'); // ignored, doesn't pass logging threshold of 'main'
console.getLogger('main.subB').info('Hello %s!', 'brave new world', { 'extra': ['pass-through params'] });

console.log('-------------------------------');

console.getLogger('main.subB').trace('trace level logging');
console.getLogger('main.subB').debug('debug level logging');
console.getLogger('main.subB').log('log level logging');
console.getLogger('main.subB').info('info level logging');
console.getLogger('main.subB').warn('warn level logging');
console.getLogger('main.subB').error('error level logging');

console.log('-------------------------------');

consoleLogger.prefixPattern = '%s (%s) -->';
console.getLogger('main.subB').trace('check this prefix pattern');
consoleLogger.prefixPattern = '<<[%2$s]>>';
console.getLogger('main.subB').trace('or this one with just the context');
consoleLogger.prefixPattern = '[%2$s] (%1$s) >';
consoleLogger.datetimePattern = 'HH:mm:ss';
console.getLogger('main.subB').trace('what about switching the context and 24h timestamp?');