Skip to content

Commit

Permalink
Merge pull request #12161 from TheCodby/master
Browse files Browse the repository at this point in the history
feat(common): added "fatal" as a log level (issue #11945)
  • Loading branch information
kamilmysliwiec committed Aug 18, 2023
2 parents 7a5b6e4 + 2cb5f2c commit 41790f8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const DEFAULT_LOG_LEVELS: LogLevel[] = [
'warn',
'debug',
'verbose',
'fatal',
];

const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
Expand Down Expand Up @@ -145,6 +146,23 @@ export class ConsoleLogger implements LoggerService {
this.printMessages(messages, context, 'verbose');
}

/**
* Write a 'fatal' level log, if the configured level allows for it.
* Prints to `stdout` with newline.
*/
fatal(message: any, context?: string): void;
fatal(message: any, ...optionalParams: [...any, string?]): void;
fatal(message: any, ...optionalParams: any[]) {
if (!this.isLevelEnabled('fatal')) {
return;
}
const { messages, context } = this.getContextAndMessagesToPrint([
message,
...optionalParams,
]);
this.printMessages(messages, context, 'fatal');
}

/**
* Set log levels
* @param levels log levels
Expand Down Expand Up @@ -330,6 +348,8 @@ export class ConsoleLogger implements LoggerService {
return clc.red;
case 'verbose':
return clc.cyanBright;
case 'fatal':
return clc.bold;
default:
return clc.green;
}
Expand Down
30 changes: 29 additions & 1 deletion packages/common/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isLogLevelEnabled } from './utils';
/**
* @publicApi
*/
export type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose';
export type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose' | 'fatal';

/**
* @publicApi
Expand Down Expand Up @@ -37,6 +37,11 @@ export interface LoggerService {
*/
verbose?(message: any, ...optionalParams: any[]): any;

/**
* Write a 'fatal' level log.
*/
fatal?(message: any, ...optionalParams: any[]): any;

/**
* Set log levels.
* @param levels log levels
Expand Down Expand Up @@ -185,6 +190,19 @@ export class Logger implements LoggerService {
this.localInstance?.verbose?.(message, ...optionalParams);
}

/**
* Write a 'fatal' level log.
*/
fatal(message: any, context?: string): void;
fatal(message: any, ...optionalParams: [...any, string?]): void;
@Logger.WrapBuffer
fatal(message: any, ...optionalParams: any[]) {
optionalParams = this.context
? optionalParams.concat(this.context)
: optionalParams;
this.localInstance?.fatal?.(message, ...optionalParams);
}

/**
* Write an 'error' level log.
*/
Expand Down Expand Up @@ -241,6 +259,16 @@ export class Logger implements LoggerService {
this.staticInstanceRef?.verbose?.(message, ...optionalParams);
}

/**
* Write a 'fatal' level log.
*/
static fatal(message: any, context?: string): void;
static fatal(message: any, ...optionalParams: [...any, string?]): void;
@Logger.WrapBuffer
static fatal(message: any, ...optionalParams: any[]) {
this.staticInstanceRef?.fatal?.(message, ...optionalParams);
}

/**
* Print buffered logs and detach buffer.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const LOG_LEVEL_VALUES: Record<LogLevel, number> = {
log: 2,
warn: 3,
error: 4,
fatal: 5,
};

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/injector/helpers/silent-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export class SilentLogger extends Logger {
warn = noop;
debug = noop;
verbose = noop;
fatal = noop;
setLogLevels = noop;
}

0 comments on commit 41790f8

Please sign in to comment.