Home

Awesome

PRHONE Log

npm version Build Status prhone

Simple JavaScript logger.

A simple JavaScript logger for client side applications but it can be used in other environments. Supports CommonJS, AMD and standalone.

See example.html and example.js for browser and node.js usage respectively.

Install

Webpack, Browserify and Node.js:

npm install prhone-log

NPM CDN:

<script src="https://unpkg.com/prhone-log"></script>

In browser it is found as window.prhone.Log.

Use

const Log = require('prhone-log');

const log1 = new Log('app1');

log1.info('Initializing app');
log1.debug('Loading global configuration');
log1.debug('Loading user configuration');

...will output:

18:31:55.987 INFO [app1] Initializing app
18:31:55.987 DEBUG [app1] Loading global configuration
18:31:55.988 DEBUG [app1] Loading user configuration

API

Log

logger Log(String namespace[, Object settings])

Creates a new logger instance.

LevelPriority
error0
warn1
info2
debug3

Object Log.COLORS

List of colors to set up levels. Available colors: RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, BLACK and WHITE.

Object Log.getSettings()

Get a copy of the current global settings.

Log.update(Object settingsToUpdate)

Update the current global settings.

Log.addLevel(Object level)

Adds a new log level globally.

Example:

const Log = require('prhone-log');

Log.addLevel({
  name: 'fatal',
  priority: 0,
  color: Log.COLORS.RED
});

const log2 = new Log('app2');

const errMsg = 'settings file is corrupt';

log2.fatal('The application crashed, details:', errMsg);
// 20:02:50.753 FATAL [app2] The application crashed, details: settings file is corrupt

logger

logger.<<method>>(...parameters [, Object meta])

Call a log level method, be it built in (debug, info, warn, error) or custom with parameters with any type (except functions). If the last parameter is an object or array it will be saved as metadata in history.

const logger = new Log('app1');
logger.info('This is a warning', { details: 'here the details' });
// 18:20:15.985 INFO [app1] This is a warning

Array logger.getHistory()

This is an array with all messages recorded in order chronological, whether logged in console or not. Only if the setting history is enabled.

The format of each message is:

{

}

Object logger.getSettings()

Get a copy of the current logger settings.

logger.update(Object settingsToUpdate)

Update the current logger settings.

logger.addLevel(Object level)

Works the same way as Log.addLevel() method, but adds the level only to this logger.

Changelog

Read CHANGELOG.md file to see changes.

License

MIT