universal-logger

A universal logging library for Node and the browser.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
universal-logger
1101.0.16 years ago6 years agoMinified + gzip package size for universal-logger in KB

Readme

universal-logger build status Coverage Status
NPM
A universal logging library for Node and the browser image

Installation

npm install --save universal-logger

Browser

Use universal-logger-browser to display logs in the browser console.
npm install --save universal-logger-browser

Usage

import emoji from 'node-emoji';
import logger, { TRACE, DEBUG, INFO, WARN, ERROR, OFF } from 'universal-logger';
import { minimal } from 'universal-logger-browser';

const log = logger() // Returns the global logger instance
    .use(minimal())
    .on('log', (context, messages) => {
        // Custom log processing
    });

log.disableStackTrace();
log.setLevel(TRACE);

log.log(INFO, 'The logger has initialized');
log.trace(emoji.get('mostly_sunny'));
log.debug(emoji.get('sun_small_cloud'));
log.info(emoji.get('barely_sunny'));
log.warn(emoji.get('rain_cloud'));
log.error(emoji.get('lightning_cloud'));

image

Log Level

log.getLevel();
log.setLevel(TRACE);
log.setLevel(OFF); // Turn off logging

Custom Log Level

import logger, { defineLogLevel } from 'universal-logger';
import { minimal } from 'universal-logger-browser';

const SILLY = defineLogLevel('silly', 0);
const VERBOSE = defineLogLevel('verbose', 1);
const INFO = defineLogLevel('info', 2);
const WARN = defineLogLevel('warn', 3);
const ERROR = defineLogLevel('error', 4);
const FATAL = defineLogLevel('fatal', 5);

const log = logger()
    .use(minimal());

log.setLevel(SILLY);
log.log(SILLY, 'Custom Log Level');

Enable/Disable Stack Trace

log.enableStackTrace();
log.disableStackTrace();

Custom Log Processing

log.on('log', (context, messages) => {
    // Custom log processing
});

Namespace

image
import emoji from 'node-emoji';
import logger, { DEBUG } from 'universal-logger';
import { minimal } from 'universal-logger-browser';

const namespace = emoji.get('rainbow');
const contextLog = logger(namespace) // Returns a logger instance with the given namespace
    .use(minimal())
    .on('log', (context, messages) => {
        console.log('Custom log processing:', context, messages);
    });

contextLog.enableStackTrace();
contextLog.setLevel(DEBUG);

contextLog.trace(emoji.get('mostly_sunny'));
contextLog.debug(emoji.get('sun_small_cloud'));
contextLog.info(emoji.get('barely_sunny'));
contextLog.warn(emoji.get('rain_cloud'));
contextLog.error(emoji.get('lightning_cloud'));

Browser Logging

https://github.com/cheton/universal-logger-browser

Minimal

image
import logger, { TRACE } from 'universal-logger';
import { minimal } from 'universal-logger-browser';

const log = logger()
    .use(minimal());

log.enableStackTrace();
log.setLevel(TRACE);

log.log(INFO, 'The logger has initialized');
log.trace(emoji.get('mostly_sunny'));
log.debug(emoji.get('sun_small_cloud'));
log.info(emoji.get('barely_sunny'));
log.warn(emoji.get('rain_cloud'));
log.error(emoji.get('lightning_cloud'));

Styleable

image
import logger, { TRACE } from 'universal-logger';
import { styleable } from 'universal-logger-browser';

const log = logger()
    .use(styleable({
        showTimestamp: true
    }));

log.enableStackTrace();
log.setLevel(TRACE);

log.log(INFO, 'The logger has initialized');
log.trace(emoji.get('mostly_sunny'));
log.debug(emoji.get('sun_small_cloud'));
log.info(emoji.get('barely_sunny'));
log.warn(emoji.get('rain_cloud'));
log.error(emoji.get('lightning_cloud'));

Plugins

License

MIT