Giter Club home page Giter Club logo

munia's Introduction

munia

Simple and practical JSON Logger

A JSON logger that can work very well without configuration, though it can be customised for standard logging options.

Installation

npm install munia --save

Usage

  • Basic usage:
const log = require('munia')()
log.info('simple and practical logger')

Output:

{"time":"2000-01-01T05:30:00.000+05:30","level":"info", "message":"simple and practical logger", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123}

  • Pass module name:
const munia = require('munia')
const log = munia('munia')
log.info('i can print module name')

Output:

{"time":"2000-01-01T05:30:00.000+05:30","level":"info", "message":"i can print module name", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123,"module":"munia"}

  • Time in local time zone (default):
const log = munia({timeFormat: 'local'})
log.info('time as local time zone')

Output:

{"time":"2000-01-01T05:30:00.000+05:30", "level":"info", "message":"time as local time zone", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123}

  • Time as ISOString:
const log = munia({timeFormat: 'ISO'})
log.info('time as ISOString format')

Output:

{"time":"2000-01-01T00:00:00.000Z", "level":"info", "message":"time as ISOString format", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123}

  • Time as epoch:
const log = munia({timeFormat: 'epoch'})
log.info('time as epoch')

Output:

{"time": 946684800000, "level":"info", "message":"time as epoch", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123}

  • Log with meta info:
const log = munia()
log.info('log userId also', {userId: 'foo'})

Output:

{"time":"2000-01-01T05:30:00.000+05:30","level":"info", "message":"log userId also", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123, "userId":"foo"}

  • Do not include process id, host name and host ip. Any of these can be set as false.
const log = munia({hostname: false, hostip: false, pid: false})
log.info('lean log')

Output:

{"time":"2000-01-01T05:30:00.000+05:30","level":"info","message":"lean log"}

  • log time taken in execution like console.time and console.timeEnd
const log = munia()
child = log.child(null, {enableTimeTaken:true})

child.info.time('API request')
setTimeout(() => {
  child.info.timeEnd('API request')
}, 1000)

Output:

{"time": "2000-01-01T05:30:00.000+05:30", "level":"info", "message": "API request", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123,"timetaken":"Initiated"}

{"time": "2000-01-01T05:30:00.000+05:30", "level":"info", "message": "API request [time taken: 1004.496987ms]", "hostname":"my-machine", "hostip":"127.0.0.1", "pid":123, "app":"munia", "timetaken":1004.496987}

  • Custom log levels, default levels are ['error', 'warn', 'info', 'http', 'verbose', 'debug', 'silly'] in priority order.
const log = munia({
  levels: ['error', 'info', 'debug'],
  logLevel: 'debug'  //default log level is info
})
log.error('this is error message')
log.info('this is info message')
log.debug('this is debug message')

Output

{"time":"2000-01-01T05:30:00.000+05:30","level":"error","message":"this is error message","hostname":"my-machine","hostip":"127.0.0.1","pid":123}
{"time":"2000-01-01T05:30:00.000+05:30","level":"info","message":"this is info message","hostname":"my-machine","hostip":"127.0.0.1","pid":123}
{"time":"2000-01-01T05:30:00.000+05:30","level":"debug","message":"this is debug message","hostname":"my-machine","hostip":"127.0.0.1","pid":123}

Test

npm test

To be implemented

  • Logger function after silly "limit" that run only n times. It would be helpful for debugging in for or while loop.

License

Licensed under MIT.

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.