Skip to content

Commit

Permalink
New: adds logErrors === deepest #20
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Sep 23, 2021
1 parent ea4b41a commit d43b467
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Next values could also be passed to constructor config, but are customizable fro
* **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.
* **logErrors**: next options available:
- `deepest`: log only the deepest occurrence of the error. This option prevents 'error spam'
* **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
29 changes: 18 additions & 11 deletions src/decorators/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
prepareData({ context, methodName }) {
const {
logger,
level,
paramsSanitizer,
resultSanitizer,
errorSanitizer,
Expand All @@ -32,15 +31,15 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
const basicLogObject = {
service : this.config.serviceName,
method : methodName,
application : this.name,
level
application : this.name
};

const buildLogObject = obj => {
const buildLogObject = (level, obj) => {
const { args, result, error, time } = obj;

return cleanUndefined({
...basicLogObject,
level,
params : paramsSanitizer(args),
result : result && resultSanitizer(result),
error : error && errorSanitizer(error),
Expand All @@ -52,7 +51,7 @@ export default class FunctionDecorator extends BaseFunctionDecorator {

const log = (logLevel, data) => {
const lev = buildLogLevel(logLevel, data);
const dat = buildLogObject(data);
const dat = buildLogObject(lev, data);

if (isFunction(logger)) return logger(lev, dat);
if (isFunction(logger[lev])) return logger[lev](dat);
Expand Down Expand Up @@ -84,12 +83,20 @@ export default class FunctionDecorator extends BaseFunctionDecorator {
}

onError({ error, log, config, time, context, params }) {
log(config.errorLevel, {
error,
args : params,
time,
context
});
const { logErrors, errorLevel } = config;

const isFirstOnly = logErrors && logErrors === 'deepest';

if (!isFirstOnly || !error[_decorated]) {
log(errorLevel, {
error,
args : params,
time,
context
});
}

if (isFirstOnly) error[_decorated] = true; // eslint-disable-line no-param-reassign

throw error;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/package/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,36 @@ test('Positive: function errorsOnly', function () {
assert.include(logger.stack.error[0].error, error.toString());
assert.isEmpty(logger.stack.info);
});

test('Positive: logErrors === deepest', function () {
const logger = new Logger();
const decorator = new Decorator({ logger, logErrors: 'deepest' });
const error = new Error('Error occured');

@decorator()
class Bugged {
first() {
this.second();
}

second() {
this.third();
}

third() {
throw error;
}
}

const bug = new Bugged();

assert.throws(() => bug.first(), error);
assert.lengthOf(logger.stack.error, 1);
assert.deepOwnInclude(logger.stack.error[0], {
service : 'Bugged',
method : 'third',
level : 'error'
});
assert.include(logger.stack.error[0].error, error.toString());
assert.isEmpty(logger.stack.info);
});

0 comments on commit d43b467

Please sign in to comment.