How can I get the request body from error logs?

Hi,

We want to structure logs and send them to an external logging service, for all requests (both successful and failed ones).

Instead of writing our own logs in hooks, for all collections and methods, we tried using the logger override and printing the message to the console.

We use the following code:

logger: (level, message, error) => {
    console.log('erorrrrrrr', error);
}

The image below shows the logged error and the exception from our custom-thrown error.

Context

  • Project name: PayAtt Internal Admin Portal
  • Team name: All teams
  • Environment name: Development (new)
  • Agent (forest package) name & version: “@forestadmin/agent”: “^1.16.2”, “@forestadmin/datasource-mongoose”: “^1.5.1”
  • Database type: MongoDB
  • Recent changes made on your end if any: Migrating to a new agent

Hello,
This is the debug log. It is never displayed in production. In the options, if you set isProduction to true, the debug log will be disabled. :pray:

1 Like

Hi,

I think you misunderstood, we would like to have the logs in production, but redirect them to our logging service, including the “data” field. Is this possible?

Kind regards,
Zakarias

Hello,
I’m adding it as a very priority bug in our board to fix it. In fact, the logger should also take this kind of error into account.

EDIT:
After a discussion, you can handle all the errors by adding your handler in the customizeErrorMessage option. Tell me if it is ok for you :pray:

Hi @Alban_Bertolini,

We tried with the customizeErrorMessage, but it does not behave like we want it.

  • Model validation fail → Hook is not run
  • Custom error thrown → Hook is not run
  • throw new Error(...) → Hook runs, but the error does not contain the body of the request, only a stack trace.

In other words, the customizeErrorMessage does not seem to work I’m afraid.

:face_with_head_bandage:
For the customizeErrorMessage I can do a PR to pass the http context as argument. You will have all the http request properties.
For the model and the custom validations why do you want to handle the errors because the methods are designed for the frontend validation? Do you wan to handle the errors when a user has a validation form error?

Passing the http context to the customizeErrorMessage will not solve the problem since it is not triggered in many cases, such as model validation failure or when context.throwValidationError is thrown, in the before hook.

Is it not possible to add the data field in the logger? As you can see in my first image, it says data: undefined, shouldn’t it contain the data of the request? This would solve our problem.

What we are trying to achieve, is to log ALL requests (including request data, and potential errors). Technically, we could achieve this by manually adding logs in all ‘before’ and ‘after’ hooks, for all of our collections, but this would be much more work than using your logger, for example.

1 Like

Hello,
After long internal discussion, we will push your feedback as a feature request.
Sorry for the inconvenience, we will try to respond quickly to your need :pray:

All right, thanks for the help @Alban_Bertolini.