node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

logor

logor

© 2016, Onur Yıldırım (@onury). MIT License.

Simple logging utility with levels, for Node.js.
This also brings some nice formatting, colors and JSON capabilities to your logs.
npm i logor

Usage

var log = require('logor').config({
    level: 2, // log.Level.INFO 
    colors: true,
    format: '{DATE} {TIME}  {LEVEL}\t{*}'
});
 
log.info('Output colorful logs with date/time and level info.');
 

Output Example

Example Screenshot

Log Levels and Methods

Level Method Color Details
ERROR 0 .error() Red Error logs. Always output to the console.
WARN 1 .warn() Yellow Warning logs.
INFO 2 .info() Cyan Informational logs.
.log() None Regardless the config, output has no colors, date/time or level info. Clean text.
.json() Cyan Safely JSON stringifies the arguments. Handles circular references.
.jsonf() Cyan Same as .json() method, but with formatting.
DEBUG 3 .debug() Magenta Debug logs.
.trace() None Prints the message to stderr with stack trace to the current position in the code.
VERBOSE 4 .verbose() Gray Silly/verbose logs.

Configuration

Pass an options object to log.config() method, with the following properties:

Option Type Default Description
level Number 4 Logging level. Levels are prioritized from 0 to 4 (highest to lowest). See Log Levels.
colors Boolean true Whether colors should be enabled.
format String|Function '{*}' Format of the log output. Takes placeholders: DATE, TIME, LEVEL and *. See Formatting Output.

Set all options at once:

log.config({
    level: 2,
    colors: true,
    format: '{*}'
});

Set log level:

log.level = 2;
// When you set level to `2` (`INFO`), 
// levels with greater values will not output any logs. 
log.debug('This will not be logged.');

Enable/disable all colors:

log.colors = false;
log.info('Log without colors');
// —> Log without colors 

Formatting Output

{} around a placeholder indicates that colors should be applied for that component. e.g. magenta if this is a debug log. Use () instead, to gray-out that component; or [] for no colors.

{color} » (gray) » [none]

Placeholder Details
* Actual argument(s) being logged. If omitted, it will be appended at the end.
DATE Short ISO date.
TIME Time in HH:mm:ss format.
LEVEL Name of the current logging level.

Logs with date/time:

log.format = '{DATE} {TIME}\t{*}';
log.info('Log with full date/time');
// —> 2016-08-18 15:30:43   Log with full date/time 

Logs with level info:

log.format = '{TIME}\t{LEVEL}\t{*}';
log.info('Log with time and level');
// —> 15:30:43   INFO    Log with time and level 
log.format = '(TIME)\t{LEVEL}\t[*]';
log.info('This is the default color of the console.');
// —> 15:30:43   INFO   This is the default color of the console. 

JSON Logs

Use log.json() to output a log with stringified argument(s).
Use log.jsonf() to also format with indents.
Use log.s() to stringify an individual or multiple values.
Use log.sf() to also format with indents.

This also handles circular references.

log.json({ info: 'this is an object' });
log.jsonf({ jsonf: 'this is a formatted json.' });
log.error(log.s({ 'stringify-arg': 'log.error(log.s(object))' }));
log.warn(log.sf({ 'stringify-format': 'log.warn(log.sf(object))' }));

Change-Log

v1.0.1 (2016-09-05)

  • Return self from .config() method.

v1.0.0 (2016-08-20)

  • initial release.

License

MIT.