cmild

1.2.1 • Public • Published

Class methods invocation logging decorator

License Build Status Coverage Status

Description

Decorator for simply and colorful (in Chrome, FF and Node) logging class methods invocation, returned results and possible errors. colors.js is using for Node.

Installation

yarn add cmild

Testing

yarn test:jest # Runs Jest with coverage collection 
yarn test:coverage # Sends coverage to .coveralls.io 
yarn test # yarn test:jest && yarn test:coverage 

Usage

You may decorate your class with default styles params, which are:

{
  styles: {
    browser: {
      invocation: 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
      result: 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
      error: 'background: #F2EAFF; color: #F20404; font-weight: bold',
    },
    node: {
      invocation: 'bgBlack.blue.bold',
      result: 'bgBlack.green.bold',
      error: 'bgBlack.red.bold',
    }
  }
}

Or set your own styles by passing object as a first argument of decorator, for example:

@Cmild({
  styles: {
    browser: {
      invocation: 'background: #F2EAFF; color: #FFEF00; font-weight: bold',
    }
  },
})

Also you may disable decorator in specific env/build, for example:

@Cmild({
  enabled: process.env.NODE_ENV !== production,
})

Common usage:

import Cmild from 'cmild'
 
 
@Cmild
class TestClass {
  testMethod1 (testArg = '') {
    return testArg
  }
  async testMethod2 (testArg1 = '', testArg2 = '') {
    return `${testArg1} ${testArg2}`
  }
  erroredMethod1 (erroredArg = '') {
    throw new Error(this.testMethod1(erroredArg))
  }
  async erroredMethod2 (erroredArg1 = '', erroredArg2 = '') {
    throw new Error(await this.testMethod2(erroredArg1, erroredArg2))
  }
}
 
const instance = new TestClass()
 
// Frist method invocation and result printing
console.log(instance.testMethod1('testArg'))
 
// Second method invocation and result printing when returned promise is resolved
instance.testMethod2('testArg1', 'testArg2').then(console.log)
 
// Third method invocation and error catching
try {
  instance.erroredMethod1('erroredArg')
} catch (error) {
  console.error(error)
}
 
// Fourth method invocation and error cathing when returned promise is rejected
instance.erroredMethod2('erroredArg1', 'erroredArg2').catch(console.error)

Dependents (0)

Package Sidebar

Install

npm i cmild

Weekly Downloads

0

Version

1.2.1

License

MIT

Unpacked Size

10.3 kB

Total Files

5

Last publish

Collaborators

  • lordotu