logerator
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

LOGERATOR

NpmVersion Travis (.org) branch npm GitHub pull requests GitHub issues

Logerator is a class decorator to log all function calls from a class. Adding console logs to every method on a class is time-consuming and produces messy code, so logerator aims to relieve the console.logs intruding on code by logging the start and end of a function as well as the result of the function call.

Installation

$ npm install --save logerator

Usage

Default: console.log

By default, logerator uses console.log as its logging function. Below is an example output using the default logging option.

import { log } from 'logerator';

@log()
export class Comic {
    private _title: string = 'myTitle';

    public getTitle() {
        return this._title;
    }
}

Output

START: Comic.getTitle()
---- RESULT ----
myTitle
END: Comic.getTitle()

Custom: logWithDate

Optionally, you can pass a custom logging function that follows the LogFunction type, or (message: string) => void. The example below uses a custom function that logs the date before the provided message.

import { log } from 'logerator';

function logWithDate(msg) {
    console.log(`${new Date()} - ${msg}`)
}

@log({ logFunction: logWithDate })
export class Comic {
    private _title: string = 'myTitle';

    public getTitle() {
        return this._title;
    }
}

Output

2019-09-13T02:21:04.681Z - START: Comic.getTitle()
2019-09-13T02:21:04.681Z - ---- RESULT ----
2019-09-13T02:21:04.681Z - myTitle
2019-09-13T02:21:04.681Z - END: Comic.getTitle()

configure

You can also configure the global default options using the configure() function. The function takes one parameter that conforms to the Options interface. The example below uses our logWithDate log function from above for all decorated classes. The instance options do override the global options.

import { configure, Options } from 'logerator';

function logWithDate(msg) {
    console.log(`${new Date()} - ${msg}`)
}

const opts: Options = {
    logFunction: logWithDate
}

configure(opts)

Options

  • logFunction - The log function to use instead of console.log

Todo

  • Add log decorator for methods

Package Sidebar

Install

npm i logerator

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

28.2 kB

Total Files

25

Last publish

Collaborators

  • chrissantos