Skip to content

Commit

Permalink
New: adds errorsOnly option (closes #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Sep 23, 2021
1 parent 255a46e commit ea4b41a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Next values could also be passed to constructor config, but are customizable fro
* **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. The 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. The function will receive logged data and should return log-level as *string*.
* **errorsOnly** - if set to *true* logger will catch only errors.
* **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)
Expand Down Expand Up @@ -194,6 +195,10 @@ default *logger-decorator* sanitizers are:
* ```simpleSanitizer``` - default [inspect](https://nodejs.org/api/util.html#util_util_inspect_object_options) function
* ```dataSanitizer``` - firstly replace all values with key ```%password%``` replaced to ```***```, and then ```simpleSanitizer```.

**Performance notes**

`dataSanitizer` could impact performance greatly on complex objects and large arrays. If you don't need to sanitize sensitive patterns (tokens, passwords), use simpleSanitizer, or write a custom handler.

### Logger

*Logger* can be a function, with the next structure:
Expand Down
14 changes: 8 additions & 6 deletions src/decorators/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
}

onSuccess({ result, log, config, time, context, params }) {
log(config.level, {
result,
args : params,
time,
context
});
if (config.level && !config.errorsOnly) {
log(config.level, {
result,
args : params,
time,
context
});
}

return result;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/package/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ test('Negative: different errorLevels on different errors', function () {
assert.notEmpty(logger.stack.error);
assert.isEmpty(logger.stack.warn);
});

test('Positive: function errorsOnly', function () {
const logger = new Logger();
const decorator = new Decorator({ logger, errorsOnly: true });

const error = new Error('n should be positive');
const decorated = decorator()(function (n) {
if (n > 0) return n * 2;
throw error;
});

assert.throws(decorated.bind(null, -5), error);
decorated(10);
assert.include(logger.stack.error[0].error, error.toString());
assert.isEmpty(logger.stack.info);
});
17 changes: 17 additions & 0 deletions tests/performance/classes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ test('class-logger with inspect sanitizers', async function () {

assert.isAtMost(result, seeds.default + allowedPerformanceLossesMS);
});

test('class-logger errorsOnly', async function () {
const decorator = new Decorator({
logger,

errorsOnly : true,
timestamp : true
});

@decorator()
class Tester extends PerformanceTester {}
const runner = new Tester();

const result = await bench(() => runner.timer(5, seeds.largeArray));

assert.isAtMost(result, seeds.default + 4);
});

0 comments on commit ea4b41a

Please sign in to comment.