Skip to content

cheton/universal-logger-browser

Repository files navigation

universal-logger-browser build status Coverage Status

NPM

Browser plugins for universal logger

Minimal

image

Styleable

image

Installation

npm install --save universal-logger universal-logger-browser

Usage

import emoji from 'node-emoji';
import logger, { TRACE, INFO } from 'universal-logger';
import { minimal, styleable } from 'universal-logger-browser';

const log = logger()
    .use(minimal({
        showSource: true,
        useNativeConsoleMethods: true
    }))
    .use(styleable({
        showSource: true,
        showTimestamp: true,
        style: {
            level: {
                silly: { // Custom log level
                    backgroundColor: '#FFF',
                    border: '1px solid #222',
                    color: '#222',
                    lineHeight: 2,
                    padding: '2px 5px'
                }
            }
        }
    }))
    .on('log', (context, messages) => {
        // Custom log processing
    });

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'));

API

Minimal

Name Type Default Description
formatter function(context, messages) Custom log formatter.
showSource boolean true Show the source line number of the caller.
Note that you need to call log.enableStackTrace() to capture stack frames.
useNativeConsoleMethods boolean true Whether to use native console methods for trace, debug, info, warn, and error.

Styleable

Name Type Default Description
colorized boolean true Show colorized output.
showSource boolean true Show the source line number of the caller.
Note that you need to call log.enableStackTrace() to capture stack frames.
showTimestamp boolean false Show timestamp.
formatTimestamp function(timestamp) Convert timestamp to string.
style object See styleable-style.js Custom styles.

License

MIT