How do I prevent Nest.js from logging an exception to the console?

Viewed 945

Background

We use ApolloHandler to handle the exceptions in our Nest.js + GraphQL application.

Problem

Although ApolloHandler manages to create a structured GraphQL error response, every exception (plus it stack trace) also generates a console log and a logger entry [ExceptionHandler], polluting the application log with thousands of already managed input errors.

Question

How to set Nest.js to supress those ApolloHandler exceptions? Of course non ApolloHandler exceptions should remain logged.

1 Answers

Create your own custom logger to filter out those messages like:


export class AppLogger extends Logger {

  error(message: string, trace: string, context?: string) {
    if (message !== 'Validations failed!') {
      super.error(message, trace, context)
    }
  }

}

And use it as

app.useLogger(new AppLogger())
Related