|
1 | | -# simplelog |
| 1 | +# simplelog-decorator |
2 | 2 |
|
3 | 3 |  |
4 | 4 | [](https://www.npmjs.com/package/simplelog-decorator) |
5 | 5 | [](https://app.codecov.io/github/devsheva/simplelog) |
6 | 6 |
|
7 | | -A simple logger decorator amplify like |
| 7 | +A simple logger built as class decorator, amplify like. |
8 | 8 |
|
9 | | -## Important |
| 9 | +## Quick Start |
10 | 10 |
|
11 | | -**Experimental decorators** are used for this package implementation and since experimental it has some bugs like unsafe typing. |
| 11 | +### Usage |
| 12 | + |
| 13 | +Due to the experimental decorators feature, there are some limitations. |
| 14 | +Starting from the preferred syntax `@Logger()` |
| 15 | + |
| 16 | +In TypeScript you need to simply enable `experimentalDecorators: true` in your `tsconfig.json`, to be able to use the preferred syntax. |
| 17 | + |
| 18 | +In JavaScript you need to use external libraries like [@babel/plugin-proposal-decorators](https://www.npmjs.com/package/@babel/plugin-proposal-decorators) to use the preferred syntax. |
| 19 | + |
| 20 | +Anyway since a decorator is just a function, you can use it by importing the `loggerDecorator` method. |
| 21 | + |
| 22 | +Arguments of logger are: |
| 23 | + |
| 24 | +- `name`: optional, default value is name of class |
| 25 | +- `options`: |
| 26 | + - `level`: default to `info` |
| 27 | + |
| 28 | +```ts |
| 29 | +import Logger, {loggerDecorator} from 'simplelog-decorator' |
| 30 | + |
| 31 | +// @Logger('YourCustomClass') |
| 32 | +// @Logger('YourCustomClass', {level: 'warn'}) |
| 33 | +// loggerDecorator()(YourClass) |
| 34 | +@Logger() // name will be YourClass, level info |
| 35 | +class YourClass { |
| 36 | + sample_method() { |
| 37 | + this.logger.verbose('hi from sample_method') |
| 38 | + this.logger.debug('hi from sample_method') |
| 39 | + this.logger.info('hi from sample_method') |
| 40 | + this.logger.warn('hi from sample_method') |
| 41 | + this.logger.error('hi from sample_method') |
| 42 | + |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +``` |
| 48 | + |
| 49 | +## Limitations |
| 50 | + |
| 51 | +As previously said, **experimental decorators** are used for this package implementation and since experimental it has some bugs like `unsafe typing`. |
12 | 52 | TypeScript compiler complains when it finds out a property used inside a class that calls a decorator cause it cannot infer it, so as a temporary workaround simply declare an interface |
13 | 53 |
|
14 | 54 | ```ts |
|
0 commit comments