Apify Discord Mirror

Updated 3 months ago

How to override `maxRequestRetries` error log

At a glance

The community member has a function _handleFailedRequestHandler that logs errors when a request fails and reaches the maximum number of retries. The community member wants to override the default log message and only display a notification that there is an error, with details available under an ID in a database. They are considering disabling the error logs and handling them manually.

In the comments, another community member suggests using a proxy class to override the log method and not display the error level in the console, but instead use a "soft fail" level to display the error logs.

There is no explicitly marked answer in the post or comments.
there is a function
Plain Text
    protected async _handleFailedRequestHandler(crawlingContext: Context, error: Error): Promise<void> {
        // Always log the last error regardless if the user provided a failedRequestHandler
        const { id, url, method, uniqueKey } = crawlingContext.request;
        const message = this._getMessageFromError(error, true);

        this.log.error(`Request failed and reached maximum retries. ${message}`, { id, url, method, uniqueKey });

        if (this.failedRequestHandler) {
            await this._tagUserHandlerError(() =>
                this.failedRequestHandler?.(this._augmentContextWithDeprecatedError(crawlingContext, error), error),
            );
        }
    }


that is triggered once maxRequestRetries is done, how could I override this log message with my own? I don't want to see whole stacktrace in my logs, I just want to notify that there is an error and details can be found under some id in DB

should I disable logs for error and handle them manually?
W
2 comments
I just want to know how can I ger rid of these defaut logs...
Attachment
image.png
okey I manage to do it with my proxy class:

Plain Text
 private getLogMethod(level: LogLevel): keyof Logger {
    switch (level) {
      case LogLevel.DEBUG:
      case LogLevel.PERF:
      case LogLevel.WARNING:
      case LogLevel.ERROR: // do not display error level in console
        return 'debug'
      case LogLevel.INFO:
        return 'info'
      case LogLevel.SOFT_FAIL: // in code use this.log.softFail to  display your error logs
        return 'error'
      default:
        return 'info'
    }
  }
Add a reply
Sign up and join the conversation on Discord