Home

Awesome

<img width="350px" src="/lines-logger.png"/>

GitHub license npm version Build Status codecov contributions welcome Code Style: Google

NPM

A simple browser logger that features:

Make your logs look like this:

logs example

Installation:

With npm:

Install the logger npm install lines-logger --save.

By including a link:

<script src="https://cdn.jsdelivr.net/npm/lines-logger@2.1.2/lib/browser.js"></script>
<script>
var LoggerFactory  = linesLogger.LoggerFactory;
var loggerFactory = new LoggerFactory();
var logger = loggerFactory.getLogger('tag');
</script>

Configuration

Create logger

If you use javascript:

var LoggerFactory = require('lines-logger').LoggerFactory; // import {LoggerFactory} from 'lines-logger';
var loggerFactory = new LoggerFactory();
var logger = loggerFactory.getLogger('tag');

If you use Typescript:

import {Logger, LoggerFactory} from 'lines-logger';

let factory: LoggerFactory = new LoggerFactory();
let logger: Logger = factory.getLogger('tag');

Log anywhere in your code:

logger.log('Hello world')(); // pay attention to () in the end. `logger.log` returns a function that should be called, thus `console.log` is called from YOUR location instead of the library.
logger.debug('My array is {}, object is {}', [1,2,3], {1:1, 2:2})();

Documentation

LoggerFactory API

methoddescription
getLoggerReturns a logger object that has binded functions warn/error/log/debug/trace
setLogWarnings(LEVEL)Sets logger level see LogLevel
getSingleLoggerStyleReturns single logger function with specified style
getSingleLoggerReturns single logger function with random color (color persist if tag is the same)
getSingleLoggerColorSame as getSingleLogger but with predefined tag color
getLoggerColorSame as getLogger, but with predefined tag style

LogLevel

nameimportancedescription
log_raise_error1Log everything and if params specified in string construct mismatch actual arguments, e.g. logger.log('two params given {} {}', one_suplied)(); throw an error.
log_with_warnings2Log everything and if params specified in string construct mismatch actual arguments, e.g. logger.log('one param given {}', one_suplied, two_supplied)(); warn in console about it.
trace3Log everything.
debug4Log debug, info, warn, error only. trace won't be printed.
info5Log info, warn, error only. debug and trace won't be printed.
warn6Log warn, error only. info, debug and trace won't be printed.
error7Log error only. warn info, debug and trace won't be printed.
disable8Disable all logs completely

Logger API

methoddescription
logger.trace('Hello world')()Executes console.trace('YOUR TEXT') if log level is less\equal trace, level 3
logger.debug('Hello world')()Executes console.debug('YOUR TEXT') if log level is less\equal debug level 4
logger.log('Hello world')()Executes console.log('YOUR TEXT') if log level is less\equal info level 5
logger.warn('Hello world')()Executes console.warn('YOUR TEXT') if log level is less\equal warn level 6
logger.error('Hello world')()Executes console.log('YOUR TEXT') if log level is less\equal error level 7
logger.log('Hello {}!', 'world')()Logger allow to print params to the middle of the line, by using {}

Best practices:

var LoggerFactory = require('lines-logger').LoggerFactory;
var loggerFactory = new LoggerFactory();
window.loggerFactory = loggerFactory

Now if you need to debug your production site you can just open devtools and type loggerFactory.setLogWarnings('trace')

import { spy } from 'sinon'
var loggerSpy = spy()
new LoggerFactory('trace', {
  error: function () {
    loggerSpy(arguments)
  },
  warn: function () {
    loggerSpy(arguments)
  },
  trace: function () {
    loggerSpy(arguments)
  },
  debug: function () {
    loggerSpy(arguments)
  },
  log: function () {
    loggerSpy(arguments)
  }
})
{
  "rules": {
    "selector": "CallExpression[callee.object.name='logger']:not([parent.type='CallExpression'])",
    "message": "You must call logger.[log,warning,debug,trace,error](\"message\")() as a function 2 times"
  }
}

Contributions

This package uses only ts with target es5. I also added babel config, but it seems like it's redundant, so it's not used.