winston-decorator
v0.0.8
Published
A decorator version of the winston logger written completely in Typescript.
Downloads
232
Readme
Winston Decorator
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