From a48aaa2a24a41809bd4d40f85d43d01525ab6749 Mon Sep 17 00:00:00 2001 From: Christopher Angelo Date: Sat, 13 Jan 2024 10:37:59 +0700 Subject: [PATCH] Add fatal logging method --- README.md | 1 + src/winston.classes.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 302d0b20..3111f678 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,7 @@ export class AppModule {} debug(message: any, context?: string) log(message: any, context?: string) error(message: any, stack?: string, context?: string) +fatal(message: any, stack?: string, context?: string) verbose(message: any, context?: string) warn(message: any, context?: string) ``` diff --git a/src/winston.classes.ts b/src/winston.classes.ts index 5a61028e..2ca610e4 100644 --- a/src/winston.classes.ts +++ b/src/winston.classes.ts @@ -23,6 +23,25 @@ export class WinstonLogger implements LoggerService { return this.logger.info(message, { context }); } + public fatal(message: any, trace?: string, context?: string): any { + context = context || this.context; + + if (message instanceof Error) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { message: msg, name, stack, ...meta } = message; + + return this.logger.log({ level: 'fatal', message: msg, context, stack: [trace || stack], error: message, ...meta }); + } + + if (!!message && 'object' === typeof message) { + const { message: msg, ...meta } = message; + + return this.logger.error({ level: 'fatal', message: msg, context, stack: [trace], ...meta }); + } + + return this.logger.error({ level: 'fatal', message, context, stack: [trace] }); + } + public error(message: any, trace?: string, context?: string): any { context = context || this.context;