This project defines an API for a logging system in JavaScript. If you have ever used a logging system, then you are aware of its benefits over using something that simply prints to the console, standard out, or standard error. A logging system is configurable and allows one to determine which log messages are logged, where they are logged, and the format in which they are logged.
There are several logging systems available for the JavaScript environment and at the start of a project you may not be ready to spend the time on testing and choosing a particular logging system. This is where this logging API steps in. It has the minimal API typically used by most logging systems. By using this logging API now, it will be easier to upgrade to a full-blown logging system implementation that closely match this API.
Although this is designed to help enforce a particular logging API, it has a
basic implementation which logs the messages to the console
using the most
appropriate method. For example, a WARN level message is logged to
console.warn
.
npm install --save qc-log_api
import * as printf from 'printf';
import { Log } from 'qc-log_api';
let LOG = Log.Factory.get('example');
LOG.trace('I am a %s level message', 'TRACE');
LOG.debug('I am a %s level message', 'DEBUG');
LOG.info('I am an %s level message', 'INFO');
LOG.warn('I am a %s level message', 'WARN');
LOG.error('I am an %s level message', 'ERROR');
LOG.fatal('I am a %s level message', 'FATAL');
LOG.trace(printf, 'I am a %s level message', 'TRACE');
let field_length_between = function (field_name, min_len, max_len) {
return printf('%s must have a length between %d and %d.', field_name, min_len, max_len);
};
LOG.warn(field_length_between, 'Username', 3, 50);
LOG.warn(field_length_between, 'Password', 8, 20);
LOG.logAt(Log.Level.TRACE, '%s must have a length between %d and %d.', 'Password', 8, 20);
LOG.logAt(Log.Level.DEBUG, '%s must have a length between %d and %d.', 'Password', 8, 20);
LOG.logAt(Log.Level.INFO, '%s must have a length between %d and %d.', 'Password', 8, 20);
LOG.logAt(Log.Level.WARN, '%s must have a length between %d and %d.', 'Password', 8, 20);
LOG.logAt(Log.Level.ERROR, '%s must have a length between %d and %d.', 'Password', 8, 20);
LOG.logAt(Log.Level.FATAL, '%s must have a length between %d and %d.', 'Password', 8, 20);