0.0.8 • Published 7 years ago

winston-decorator v0.0.8

Weekly downloads
48
License
MIT
Repository
github
Last release
7 years ago

Winston Decorator

npm npm version Build Status codecov Dependencies license

A decorator version of the winston logger written completely in Typescript.

Installation

npm install winston-decorator

Usage

winston-decorator is designed to inject a winston logger on the decorated property of the given class.

For example:

import {logger, LoggerInstance} from 'winston-decorator'

class Test
{
    @logger()
    private _logger;
    
    public constructor()
    {
        this._logger.info('Now I can use the logger from here!');
    }
}

You can pass a winston.LoggerOptions object to the decorator to specify your settings:

@logger({level: 'debug'})

or you can put it on a separate settings file if you prefer to change the settings of all your loggers at once.

import settings from './settings'
class Test
{
    @logger(settings)
    private _logger;
    
    public constructor()
    {
        this._logger.info('Now I can use the logger from here!');
    }
}

The logger comes with a default label set on your class name so you can always know from where your log come from. For example:

verbose: [APIServer] Route loaded successfully
debug: [Route] Function called
verbose: [APIServer] Route loaded successfully
info: [APIServer] Listening on port 8080!

but you can change it if you want, setting it on the decorator

import settings from './settings'
class Test
{
    @logger(settings, {label: 'test'})
    private _logger;
    
    public constructor()
    {
        this._logger.info('This will output <[test] ...>');
    }
}

Test environment

The logger is designed to automatically hide all the logs when testing environment is set. To do so you have to set the process.env[NODE_ENV] variable to "test" (like this: NODE_ENV=test node main.js). But you can also opt for a custom value for the env_variable. To specify it just pass it to the decorator settings.

import settings from './settings'

class Test
{
    @logger(settings, {test_environment: 'my_custom_env'})
    private _logger;
    
    public constructor()
    {
        this._logger.info('This will be hidden');
    }
}

In this case you can run the program with NODE_ENV=my_custom_env mocha test.js and hide all the logs from the output.

Run tests

npm test

Authors

Marco Moschettini, Alessandro Petraro

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago