Skip to content

Commit 1d87f0a

Browse files
New: adds paramsLevel prop. closes #125
1 parent febfa85 commit 1d87f0a

File tree

6 files changed

+119
-52
lines changed

6 files changed

+119
-52
lines changed

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
],
55
"globals": {
66
"performance": true
7+
},
8+
"rules": {
9+
"unicorn/expiring-todo-comments": 0
710
}
811
}

README.md

+26-24
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,33 @@ class Rectangle {
9595

9696
### Configuration
9797

98-
Config must be a JavaScript ```Object``` with the following attributes:
99-
* **logger** - logger, which build decorator will use, *[console](https://nodejs.org/api/console.html)* by default, see [logger](#logger) for more details
100-
* **name** - the app name, to include in all logs, could be omitted.
101-
102-
Next values could also be passed to constructor config, but are customizable from ```decorator(customConfig)``` invokation:
103-
* **timestamp** - if set to *true* timestamps will be added to all logs.
104-
* **level** - default log-level, pay attention that logger must support it as ```logger.level(smth)```, *'info'* by default. Also *function* could be passed. The function will receive logged data and should return log-level as *string*.
105-
* **errorLevel** - level, used for errors. *'error'* by default. Also *function* could be passed. The function will receive logged data and should return log-level as *string*.
106-
* **errorsOnly** - if set to *true* logger will catch only errors.
98+
Config must be a JavaScript `Object` with the following attributes:
99+
* **logger** - the logger that the build decorator will use. *[console](https://nodejs.org/api/console.html)* by default; see [logger](#logger) for more details.
100+
* **name** - the app name to include in all logs; it could be omitted.
101+
102+
Next values could also be passed to the constructor config, but are customizable from the `decorator(customConfig)` invocation:
103+
* **timestamp** - if set to *true*, timestamps will be added to all logs.
104+
* **level** - the default log-level; pay attention that the logger must support it as `logger.level(smth)`, *'info'* by default. Also, a *function* could be passed. The function will receive logged data and should return the log-level as a *string*.
105+
* **errorLevel** - the level used for errors; *'error'* by default. Also, a *function* could be passed. The function will receive logged data and should return the log-level as a *string*.
106+
* **paramsLevel** - the level used for logging params. Logger will print input params before the function starts executing. Also, a *function* could be passed. The function will receive logged data and should return the log-level as a *string*. If omitted, nothing will be logged before execution.
107+
* **errorsOnly** - if set to *true*, the logger will catch only errors.
107108
* **logErrors**: next options available:
108-
- `deepest`: log only the deepest occurrence of the error. This option prevents 'error spam'
109-
* **paramsSanitizer** - function to sanitize input parametrs from sensitive or redundant data, see [sanitizers](#sanitizers) for more details, by default [dataSanitizer](#sanitizers).
110-
* **resultSanitizer** - output data sanitizer, by default [dataSanitizer](#sanitizers)
111-
* **errorSanitizer** - error sanitizer, by default [simpleSanitizer](#sanitizers)
112-
* **contextSanitizer** - function context sanitizer, if ommited, no context will be logged.
113-
* **dublicates** - if set to *true*, it is possible to use multiple decorators at once (see [example](#duplicates)). **Note:** `duplicates` key also works, `dublicates` will be completle renamed to `duplicates` in version 2.0.
114-
* **keepReflectMetadata** - if `logger-decorator` is used with other decorators, they can set own [reflect metadata](https://blog.bitsrc.io/typescripts-reflect-metadata-what-it-is-and-how-to-use-it-fb7b19cfc7e2). by passing `keepReflectMetadata` array, you can prevent metadata from reset. For example for nestJS its good idea to use `{ keepReflectMetadata: ['method', 'path'] }`.
115-
116-
Next parametrs could help in class method filtering:
117-
* **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)
118-
* **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)
119-
* **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)
120-
* **include** - array with method names, for which logs will be added.
121-
* **exclude** - array with method names, for which logs won't be added.
122-
* **methodNameFilter** - function, to filter method names
109+
- `deepest`: log only the deepest occurrence of the error. This option prevents 'error spam.'
110+
* **paramsSanitizer** - function to sanitize input parameters from sensitive or redundant data; see [sanitizers](#sanitizers) for more details, by default [dataSanitizer](#sanitizers).
111+
* **resultSanitizer** - output data sanitizer, by default [dataSanitizer](#sanitizers).
112+
* **errorSanitizer** - error sanitizer, by default [simpleSanitizer](#sanitizers).
113+
* **contextSanitizer** - function context sanitizer; if omitted, no context will be logged.
114+
* **duplicates** - if set to *true*, it is possible to use multiple decorators at once (see [example](#duplicates)).
115+
* **keepReflectMetadata** - if `logger-decorator` is used with other decorators, they can set own [reflect metadata](https://blog.bitsrc.io/typescripts-reflect-metadata-what-it-is-and-how-to-use-it-fb7b19cfc7e2). By passing `keepReflectMetadata` array, you can prevent metadata from resetting. For example, for NestJS, it's a good idea to use `{ keepReflectMetadata: ['method', 'path'] }`.
116+
117+
Next parameters could help in class method filtering:
118+
* **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).
119+
* **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).
120+
* **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).
121+
* **include** - array with method names for which logs will be added.
122+
* **exclude** - array with method names for which logs won't be added.
123+
* **methodNameFilter** - function to filter method names.
124+
123125

124126

125127
### Functions

package-lock.json

+35-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"eslint-plugin-scanjs-rules": "^0.2.1",
7979
"eslint-plugin-security": "^1.4.0",
8080
"eslint-plugin-sonarjs": "^0.11.0",
81-
"eslint-plugin-unicorn": "^40.1.0",
81+
"eslint-plugin-unicorn": "42.0.0",
8282
"fs-extra": "^10.0.0",
8383
"husky": "^7.0.4",
8484
"jscpd": "^3.4.5",

src/decorators/function.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
5454
result : result && resultSanitizer(result),
5555
error : error && errorSanitizer(error),
5656
context : (contextSanitizer && context) ? contextSanitizer(context) : undefined,
57-
benchmark : getBenchmark(time),
57+
benchmark : time ? getBenchmark(time) : undefined,
5858
timestamp : timestamp ? (new Date()).toISOString() : undefined
5959
});
6060
};
@@ -79,6 +79,19 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
7979
};
8080
}
8181

82+
onParams({ params, config, log, context }) {
83+
const { paramsLevel } = config;
84+
85+
if (paramsLevel) {
86+
log(paramsLevel, {
87+
args : params,
88+
context
89+
});
90+
}
91+
92+
return params;
93+
}
94+
8295
onSuccess({ result, log, config, time, context, params }) {
8396
if (config.level && !config.errorsOnly) {
8497
log(config.level, {

tests/package/params.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { assert } from 'chai';
2+
import { Decorator } from '../entry';
3+
import Logger from '../Logger';
4+
import { verifyStdout } from '../utils';
5+
6+
suite('Params');
7+
8+
test('Positive: function paramsLevel', function () {
9+
const logger = new Logger();
10+
const decorator = new Decorator({ logger, paramsLevel: 'verbose' });
11+
12+
const decorated = decorator()(function x(n) {
13+
if (n > 0) return n * 2;
14+
15+
return n;
16+
});
17+
18+
assert.equal(decorated(4), 8);
19+
assert.isNotEmpty(logger.stack.verbose);
20+
verifyStdout(logger, { params: '[ 4 ]', result: '8' }, { level: 'info' });
21+
verifyStdout(logger, { method: 'x', params: '[ 4 ]' }, { level: 'verbose' });
22+
});
23+
24+
25+
test('Negative: function paramsLevel', function () {
26+
const logger = new Logger();
27+
const decorator = new Decorator({ logger });
28+
29+
const decorated = decorator()(function (n) {
30+
if (n > 0) return n * 2;
31+
32+
return n;
33+
});
34+
35+
assert.equal(decorated(4), 8);
36+
37+
verifyStdout(logger, { params: '[ 4 ]', result: '8' }, { level: 'info' });
38+
39+
assert.isEmpty(logger.stack.verbose);
40+
});

0 commit comments

Comments
 (0)