Skip to content

Commit

Permalink
Specify foreground and background colors of console
Browse files Browse the repository at this point in the history
  • Loading branch information
ycanardeau committed May 6, 2024
1 parent 3dc381a commit 6cd6c44
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 38 deletions.
3 changes: 1 addition & 2 deletions packages/antiforgery/src/internal/DefaultAntiforgery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function logTokenDeserializeError(logger: ILogger, error: Error): void {
logger.log(
LogLevel.Error,
'An exception was thrown while deserializing the token.',
error,
);
}

Expand Down Expand Up @@ -164,7 +163,7 @@ export class DefaultAntiforgery implements IAntiforgery {
responseHeaders.getHeader(HeaderNames.Pragma) as
| string
| string[] /* REVIEw */,
)
)
: undefined;
if (pragmaHeader !== undefined && pragmaHeader.count > 0) {
// If the Pragma is already set, override it only if required
Expand Down
2 changes: 1 addition & 1 deletion packages/extensions.logging.abstractions/src/ILogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { LogLevel } from './LogLevel';

// https://source.dot.net/#Microsoft.Extensions.Logging.Abstractions/ILogger.cs,0976525f5d1b9e54,references
export interface ILogger {
log(logLevel: LogLevel, message?: any, ...optionalParams: any[]): void;
log(logLevel: LogLevel, message: string): void;
isEnabled(logLevel: LogLevel): boolean;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ILogger } from './ILogger';
import { LogLevel } from './LogLevel';

export function logDebug(
logger: ILogger,
message?: any,
...optionalParams: any[]
): void {
logger.log(LogLevel.Debug, message, ...optionalParams);
export function logDebug(logger: ILogger, message: string): void {
logger.log(LogLevel.Debug, message);
}
4 changes: 2 additions & 2 deletions packages/extensions.logging.abstractions/src/LoggerT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export class LoggerT<T> implements ILoggerT<T> {
this.logger = factory.createLogger('' /* TODO */);
}

log(logLevel: LogLevel, message?: any, ...optionalParams: any[]): void {
this.logger.log(logLevel, message, ...optionalParams);
log(logLevel: LogLevel, message: string): void {
this.logger.log(logLevel, message);
}

isEnabled(logLevel: LogLevel): boolean {
Expand Down
161 changes: 137 additions & 24 deletions packages/extensions.logging/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,146 @@
import { ConsoleColor } from '@yohira/base';
import { ILogger, LogLevel } from '@yohira/extensions.logging.abstractions';

// TODO: move
function getLogLevelString(logLevel: LogLevel): string {
switch (logLevel) {
case LogLevel.Trace:
return 'trce';
case LogLevel.Debug:
return 'dbug';
case LogLevel.Information:
return 'info';
case LogLevel.Warning:
return 'warn';
case LogLevel.Error:
return 'fail';
case LogLevel.Critical:
return 'crit';
default:
throw new Error(
'Specified argument was out of the range of valid values.' /* LOC */,
);
}
}

// TODO: move
class ConsoleColors {
constructor(
readonly foreground: ConsoleColor | undefined,
readonly background: ConsoleColor | undefined,
) {}
}

// TODO: move
function getLogLevelConsoleColors(logLevel: LogLevel): ConsoleColors {
const disableColors = false;
if (disableColors) {
return new ConsoleColors(undefined, undefined);
}
// We must explicitly set the background color if we are setting the foreground color,
// since just setting one can look bad on the users console.
switch (logLevel) {
case LogLevel.Trace:
return new ConsoleColors(ConsoleColor.Gray, ConsoleColor.Black);
case LogLevel.Debug:
return new ConsoleColors(ConsoleColor.Gray, ConsoleColor.Black);
case LogLevel.Information:
return new ConsoleColors(
ConsoleColor.DarkGreen,
ConsoleColor.Black,
);
case LogLevel.Warning:
return new ConsoleColors(ConsoleColor.Yellow, ConsoleColor.Black);
case LogLevel.Error:
return new ConsoleColors(ConsoleColor.Black, ConsoleColor.DarkRed);
case LogLevel.Critical:
return new ConsoleColors(ConsoleColor.White, ConsoleColor.DarkRed);
default:
return new ConsoleColors(undefined, undefined);
}
}

// TODO: move
const defaultForegroundColor = '\x1b[39m\x1b[22m'; // reset to default foreground color

// TODO: move
function getForegroundColorEscapeCode(color: ConsoleColor): string {
switch (color) {
case ConsoleColor.Black:
return '\x1b[30m';
case ConsoleColor.DarkRed:
return '\x1b[31m';
case ConsoleColor.DarkGreen:
return '\x1b[32m';
case ConsoleColor.DarkYellow:
return '\x1b[33m';
case ConsoleColor.DarkBlue:
return '\x1b[34m';
case ConsoleColor.DarkMagenta:
return '\x1b[35m';
case ConsoleColor.DarkCyan:
return '\x1b[36m';
case ConsoleColor.Gray:
return '\x1b[37m';
case ConsoleColor.Red:
return '\x1b[1m\x1b[31m';
case ConsoleColor.Green:
return '\x1b[1m\x1b[32m';
case ConsoleColor.Yellow:
return '\x1b[1m\x1b[33m';
case ConsoleColor.Blue:
return '\x1b[1m\x1b[34m';
case ConsoleColor.Magenta:
return '\x1b[1m\x1b[35m';
case ConsoleColor.Cyan:
return '\x1b[1m\x1b[36m';
case ConsoleColor.White:
return '\x1b[1m\x1b[37m';
default:
return defaultForegroundColor; // default foreground color
}
}

// TODO: move
const defaultBackgroundColor = '\x1b[49m'; // reset to the background color

// TODO: move
function getBackgroundColorEscapeCode(color: ConsoleColor): string {
switch (color) {
case ConsoleColor.Black:
return '\x1b[40m';
case ConsoleColor.DarkRed:
return '\x1b[41m';
case ConsoleColor.DarkGreen:
return '\x1b[42m';
case ConsoleColor.DarkYellow:
return '\x1b[43m';
case ConsoleColor.DarkBlue:
return '\x1b[44m';
case ConsoleColor.DarkMagenta:
return '\x1b[45m';
case ConsoleColor.DarkCyan:
return '\x1b[46m';
case ConsoleColor.Gray:
return '\x1b[47m';
default:
return defaultBackgroundColor; // Use default background color
}
}

// https://source.dot.net/#Microsoft.Extensions.Logging/Logger.cs,fdb90470ff3a62bd,references
export class Logger implements ILogger {
constructor(private readonly categoryName: string) {}

log(logLevel: LogLevel, message?: any, ...optionalParams: any[]): void {
switch (logLevel) {
case LogLevel.Trace:
console.trace(message, ...optionalParams);
break;
case LogLevel.Debug:
console.debug(message, ...optionalParams);
break;
case LogLevel.Information:
console.info(message, ...optionalParams);
break;
case LogLevel.Warning:
console.warn(message, ...optionalParams);
break;
case LogLevel.Error:
console.error(message, ...optionalParams);
break;
case LogLevel.Critical:
console.error(message, ...optionalParams);
break;
case LogLevel.None:
console.log(message, ...optionalParams);
break;
}
log(logLevel: LogLevel, message: string): void {
const { foreground, background } = getLogLevelConsoleColors(logLevel);

console.log(
`${getForegroundColorEscapeCode(foreground ?? ConsoleColor.White)}${getBackgroundColorEscapeCode(background ?? ConsoleColor.Black)}${getLogLevelString(logLevel)}\x1b[0m: ${this.categoryName}\n${message
.split('\n')
.map((line) => ` ${line}`)
.join('\n')}`,
);
}

isEnabled(logLevel: LogLevel): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function applicationError(
logger.log(
LogLevel.Error,
`Connection id "${connectionId}", Request id "${traceIdentifier}": An unhandled exception was thrown by the application.`,
error,
);
}

Expand All @@ -31,8 +30,8 @@ export class NodeTrace implements ILogger {
return this.generalLogger.isEnabled(logLevel);
}

log(logLevel: LogLevel, message?: any, ...optionalParams: any[]): void {
return this.generalLogger.log(logLevel, message, ...optionalParams);
log(logLevel: LogLevel, message: string): void {
return this.generalLogger.log(logLevel, message);
}

applicationError(
Expand Down

0 comments on commit 6cd6c44

Please sign in to comment.