Git Product home page Git Product logo

logger-man's Introduction

logger-man

A lightweight&useful stream based logging module for node.js

Build Status Coverage Status

installation

Via NPM:

npm install logger-man --save

Example

'use strict';
let loggerMan = require('logger-man');

let myLogger = loggerMan.get('myLogger');
myLogger.init({
    stream: process.stdout,
    level: 'INFO',
    formatter: '${this.time} ${this.name}[${this.pid}] ${this.level}: ${this.message}\n'
  });
myLogger.on('ERROR', (msg) => console.log(`get ERROR event! msg: ${msg}`));

myLogger.info('myInfo'); // 2015/June/4 pm 3:42:37 myLogger[5046] INFO: myInfo
myLogger.error('myError'); // 2015/June/4 pm 3:42:37 myLogger[5046] ERROR: myError

// => get ERROR event! msg: 2015/June/4 pm 3:42:37 myLogger[5046] ERROR: myError

Available Levels

DEBUG
INFO
WARN
ERROR
CRITICAL

If a message's level is before the level property of the object you assigned in init() method, logger-man will ignore it.

Formatter

Logger-man's formatter use the ES6 'template string' style, you can put all available attributes in ${}.

The default formatter is:

'${this.time} ${this.name}[${this.pid}] ${this.level}: ${this.message}\n'

All available attributes provided by logger-man:

this.name //logger name
this.level //record level
this.formatter //formatter string
this.pid //process id
this.createdAt //unix timestamp, when you created this logger
this.time //readable time, has the struct like : `YYYY/MMMM/D a h:mm:ss`
this.message //mesaage

API

loggerMan.get(name)

Get a logger, all loggers are saved in a Map inside of logger-man, so if you try to get a logger with the same name for many times, logger-man will return the same instance every time.

loggerMan.init(optionObj)

optionObj:

  • stream: target stream you want to write your record to
  • level: logging level
  • formatter: formatter

Initialize your logger.

loggerMan.debug(msg)/info(msg)/error(msg)/...

Logging your message with the specialized level.

loggerMan.on(level, handler)

options

  • level: record level ('INFO','ERROR'...)
  • handler: callback

Add your own listener on specialized level event.

logger-man's People

Contributors

davidcai1111 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.