Simple logger can be used as wrapper for console, adds log output levels, multiple targets and history.
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.babelrc
.editorconfig
.gitignore
.travis.yml
license
package.json
readme.md

readme.md

logger.js

Build Status Coverage Status

Simple logger can be used as wrapper for console, adds log output levels, multiple targets and history. Will default to use console, but you can add your own custom targets instead.

This library is written entirely in ECMAScript 6+, so if you need support for older browsers you need something like Babel.

API

Contructor

new Logger(logLevel, ...targets)

Parameters

logLevel

Optional. Log messages from specified level (ordered):

  • LogLevel.log
  • LogLevel.info
  • LogLevel.warn
  • LogLevel.error

Default: LogLevels.log

targets

Optional. Targets to output to, multiple can be specified: new Logger(LogLevels.log, target1, target2)

Default: console

Usage

import Logger from 'logger'

const logger = new Logger()

logger.log('log line 1')
logger.info('log line 2')
logger.warn('log line 3')
logger.error('log line 4')

Log levels

You can choose only to log above a certain level. To only print warnings and errors:

import Logger, {LogLevels} from 'logger'

const logger = new Logger(LogLevels.warn)

logger.log('log line 1')    // will not be logged
logger.info('log line 2')   // will not be logged
logger.warn('log line 3')
logger.error('log line 4')

Multiple targets

You can choose to log to multiple targets, for instance sending logs to both console and a logging server.

import Logger, {LogLevels} from 'logger'

const serverTarget = {
    log: function (...args) {
        fetch('https://logserver.com/logs', {
            method: 'post',
            body: JSON.stringify(args)
        })
    }
}

const logger = new Logger(LogLevels.log, console, serverTarget)

logger.log('log line 1')    // will be logged to both console and server

Targets need to implement the standard methods: log, info, warn and error.

History (optional target)

A prebuilt History target is included, which can be useful in an offline scenario. This allows you to send a history of logs to the server when the system comes online again:

import Logger, {LogLevels, History} from 'logger'

const target = new History()
const logger = new Logger(LogLevels.log, target)

logger.log('log line 1')    // will be saved in history

window.addEventListener('online', function () {
    fetch('https://logserver.com/logs', {
        method: 'post',
        body: JSON.stringify(target.history)
    })
})