refactor(server): auto print full stack on logger.error (#10161)

This commit is contained in:
fengmk2
2025-02-13 11:49:41 +00:00
parent 899b1d60e0
commit 3ff721abe8
5 changed files with 113 additions and 21 deletions
@@ -17,4 +17,30 @@ export class AFFiNELogger extends ConsoleLogger {
static getRequestId(): string | undefined {
return ClsServiceManager.getClsService()?.getId();
}
/**
* Nestjs ConsoleLogger.error() will not print the stack trace if the error is an instance of Error
* This method is a workaround to print the stack trace
*
* Usage:
* ```
* this.logger.error('some error happens', errInstance);
* ```
*/
override error(
message: any,
stackOrError?: Error | string | unknown,
context?: string
) {
let stack = '';
if (stackOrError instanceof Error) {
const err = stackOrError;
stack = err.stack ?? '';
if (err.cause instanceof Error && err.cause.stack) {
stack += `\n\nCaused by:\n\n${err.cause.stack}`;
}
stackOrError = stack;
}
super.error(message, stackOrError, context);
}
}