loggerjack
v0.2.0
Published
logging utilities
Downloads
7
Readme
LoggerJack
LoggerJack provides you simple with a simple but usefull namespaced logger.
npm install loggerjack
import Jack from 'loggerjack'
const Library = Jack.jack('library', 'INFO')
// create a namespaced Jack with key : 'library'
// log levels are : 'DEBUG' 'INFO' 'WARN' 'ERROR' 'NONE'
// lowercase is valid too
const comics = Library.logger('comics')
// create a namespaced logger with key : 'library.comics'
// inherit INFO log level from MyJack
comics.error('msg', {}, 0, ...)
// will log as error : library.comics: msg {} 0 ...
comics.warn('msg') // will log ...
comics.info('msg') // will log ...
comics.debug('msg') // will NOT log
const books = Library.logger('book', 'ERROR')
// override log level to DEBUG
const tests = books.logger('test', 'INFO')
// create a namespaced logger with key : 'library.book.test'
// Note : the .jack and .logger methods are the same, but typescipt will show only logging related methods on .logger result, and all on .jack result
/// now let's harness the power of Jack to change dynamically the logging level
Library.set('INFO')
// set self to INFO level
Library.set('DEBUG', 'book')
// will set key to DEBUG
Jack.set('DEBUG', 'library.book')
// is equivalent
books.debug('msg') // now logs msg
tests.debug('msg') // still at INGO level
Library
.set('INFO')
.set('DEBUG', 'book')
// .set can be chained
/// let's go a level higher and use patterns (GLOB inspired)
Library.set('INFO', '**')
// set self and every descendant to INFO level
Library.set('DEBUG', '**.other.test')
// book.test => unchanged
// other.test => DEBUG
// other.test.deep => unchanged
// deep.other.test.deep => DEBUG
Library.set('DEBUG', '*oo*')
// book => DEBUG
// book.test => unchanged
// other.test => unchanged
Library.set('DEBUG', '*oo*.**')
// book => nothing
// library.book => DEBUG
// library.book.test => DEBUG
// library.other.test => nothing
/// let's go even higher and imagine React is using loggerjack
Jack.set('DEBUG', 'react.**')
// get ready to a lot of logs...
// but it rocks !
/// Notes about logger level inheritance
Library.jack('a.b.c', 'INFO')
Library.jack('a.b', 'WARN')
Library.jack('a.b.c.d.e')
// without a level, it will take level of nearest declared parent
// in this case : INFO
// furthermore, patterns are kept in Jack memory
// so when we create a new jack, it will be checked against all patterns in parents
// starting from the nearest parent and the most recent pattern
// to the last ancestor and oldest pattern
// the check is done even if the jack is build with a level
Jack.set('DEBUG', '**.spec')
Jack.set('DEBUG', '**.test')
Library.set('NONE', '**.book.test')
Library.jack('book.shelf.spec', 'INFO')
// 'book.shelf.spec' checked against **.book.test' : false, go next
// 'library.book.shelf.spec' checked against **.test' : false, go next
// 'library.book.shelf.spec' checked against **.spec' : true, override 'INFO' by 'DEBUG'
// to sum up everything, at jack creation :
// inheritance < given level < existing pattern
/// Other notes
// Jacks are unique per key
Jack.jack('library') === Jack.jack('library') // true