Skip to content

Commit

Permalink
Chore: (docs) Readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Oct 29, 2019
1 parent 909e6bb commit 2472efa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
51 changes: 41 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,62 @@ The package provides simple decorator, so you can simply wrap functions or class
The recommended way for using 'logger-decorator' is to build own decorator singleton inside the app.

```javascript
import Decorator from 'logger-decorator';
import { Decorator } from 'logger-decorator';

const decorator = new Decorator(config);
```

Or just use decorator with default configuration:

```javascript
import log from 'logger-decorator';

@log({level: 'verbose'})
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
```

### Configuration

Config must be a JavaScript ```Object``` with the following attributes:
* **logger** - logger, which build decorator will use, *console* by default, see [logger](#logger) for more details
* **logger** - logger, which build decorator will use, *[console](https://nodejs.org/api/console.html)* by default, see [logger](#logger) for more details
* **name** - the app name, to include in all logs, could be omitted.

Next values could also be passed to constructor config, but are customizable from ```decorator(custom)``` invokation:
* **timestamp** - if set to true timestamps will be added to all logs.
* **level** - default log-level, pay attention then logger must support it as ```logger.level(data)```, *info* by default
Next values could also be passed to constructor config, but are customizable from ```decorator(customConfig)``` invokation:
* **timestamp** - if set to *true* timestamps will be added to all logs.
* **level** - default log-level, pay attention that logger must support it as ```logger.level(smth)```, *'info'* by default. Also *function* could be passed. Function will receive logged data and should return log-level as *string*.
* **errorLevel** - level, used for errors. *'error'* by default. Also *function* could be passed. Function will receive logged data and should return log-level as *string*.
* **paramsSanitizer** - function to sanitize input parametrs from sensitive or redundant data, see [sanitizers](#sanitizers) for more details, by default [dataSanitizer](#sanitizers).
* **resultSanitizer** - output data sanitizer, by default [dataSanitizer](#sanitizers)
* **errorSanitizer** - error sanitizer, by default [simpleSanitizer](#sanitizers)
* **contextSanitizer** - function context sanitizer, if ommited, no context will be logged.
* **dublicates** - if set to *true*, it is possible to use multiple decorators at once (see [example](#dublicates))

Next parametrs could help in class method filtering:
* **getters** - if set to *true*, [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) will also be logged (applied to class and class-method decorators)
* **setters** - if set to *true*, [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) will also be logged (applied to class and class-method decorators)
* **classProperties** - if set to *true*, [class-properties](https://babeljs.io/docs/en/babel-plugin-proposal-class-properties) will also be logged (applied to class decorators only)
* **include** - array with method names, for which loggs will be added.
* **exclude** - array with method names, for which loggs will not be added.
* **methodNameFilter** - function, to filter method names


### Functions

To decorate a function with ```logger-decorator``` the next approach can be applied:

```javascript
import Decorator from 'logger-decorator';
import { Decorator } from 'logger-decorator';

const decorator = new Decorator({ logger });
const decorated = decorator()(function sum(a, b) {
Expand All @@ -81,14 +112,14 @@ const decorated = log({ methodName })(sumAsync);
await decorated(5, 8); // 13
```

Besides ```methodName``` any of the ```timestamp, level, paramsSanitizer, resultSanitizer, errorSanitizer, contextSanitizer``` can be transferred to ```log(customConfig)``` and will replace global values.
Besides ```methodName``` any of the ```timestamp, level, paramsSanitizer, resultSanitizer, errorSanitizer, contextSanitizer, etc...``` can be transferred to ```log(customConfig)``` and will replace global values.

### Classes

To embed logging for all class methods follow next approach:

```javascript
import Decorator from 'logger-decorator';
import { Decorator } from 'logger-decorator';
const log = new Decorator();

@log()
Expand All @@ -107,12 +138,12 @@ class MyClass {
}
```
When needed, the decorator can be applied to a specific class method. It is also possible to use multiple decorators at once:
When needed, the decorator can be applied to a specific class method. It is also possible to use multiple decorators <a name="dublicates"></a> at once:
```javascript
const decorator = new Decorator({ logger, contextSanitizer: data => ({ base: data.base }) }); // level info by default

@decorator({ level: 'verbose', contextSanitizer: data => data.base })
@decorator({ level: 'verbose', contextSanitizer: data => data.base, dublicates: true })
class Calculator {
base = 10;
_secret = 'x0Hdxx2o1f7WZJ';
Expand Down
19 changes: 4 additions & 15 deletions src/decorators/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@ export function classMethodDecorator({ target, methodName, descriptor }, config

descriptor[key] = key === 'initializer'// eslint-disable-line no-param-reassign
? function () {
return functionDecorator(
old.call(target),
functionDecoratorConfig
);
return functionDecorator(old.call(target), functionDecoratorConfig);
}
: functionDecorator(
descriptor[key],
functionDecoratorConfig
);
: functionDecorator(descriptor[key], functionDecoratorConfig);
});

return descriptor;
Expand All @@ -51,13 +45,8 @@ export function classMethodDecorator({ target, methodName, descriptor }, config
function decorateClass(target, config) {
getMethodNames(target)
.filter(methodName => {
if (config.include?.includes(methodName)) {
return true;
}

if (config.exclude?.includes(methodName)) {
return false;
}
if (config.include?.includes(methodName)) return true;
if (config.exclude?.includes(methodName)) return false;

return isFunction(config.methodNameFilter)
? config.methodNameFilter(methodName)
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
classDecorator
} from './decorators';
import defaultConfig from './defaults';
import { sanitizeRegexp } from './utils/sanitizers';

const dfc = [ defaultConfig ];
// TODO change from log(opts)(func) to log(func, opts)

export class Decorator {
constructor(...opts) {
Expand All @@ -33,3 +33,7 @@ export class Decorator {

export default new Decorator();

export {
sanitizeRegexp
};

0 comments on commit 2472efa

Please sign in to comment.