Skip to content

Commit

Permalink
fix: cleanup logging from Context.<anonymous> and Runner.<anonymous>
Browse files Browse the repository at this point in the history
Signed-off-by: mdolhalo <[email protected]>
  • Loading branch information
mdolhalo committed Aug 22, 2023
1 parent ed57855 commit 7700995
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions tests/e2e/utils/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export abstract class Logger {
* @param indentLevel log level
*/
static error(text: string = '', indentLevel: number = 1): void {
const callerInfo: string = this.getCallerInfo();
const logLevelSymbol: string = '[ERROR] ';
this.logText(
indentLevel,
`[ERROR] ${this.getCallerInfo()} ${this.separator(text)} ${text}`
logLevelSymbol,
`${this.getFullMessage(callerInfo, text)}`
);
}

Expand All @@ -31,9 +34,12 @@ export abstract class Logger {
if (ReporterConstants.TS_SELENIUM_LOG_LEVEL === 'ERROR') {
return;
}
const callerInfo: string = this.getCallerInfo();
const logLevelSymbol: string = '[WARN] ';
this.logText(
indentLevel,
`[WARN] ${this.getCallerInfo()} ${this.separator(text)} ${text}`
logLevelSymbol,
`${this.getFullMessage(callerInfo, text)}`
);
}

Expand All @@ -49,9 +55,12 @@ export abstract class Logger {
) {
return;
}
const callerInfo: string = this.getCallerInfo();
const logLevelSymbol: string = '• ';
this.logText(
indentLevel,
`• ${this.getCallerInfo()} ${this.separator(text)} ${text}`
logLevelSymbol,
`${this.getFullMessage(callerInfo, text)}`
);
}

Expand All @@ -68,9 +77,12 @@ export abstract class Logger {
) {
return;
}
const callerInfo: string = this.getCallerInfo();
const logLevelSymbol: string = '▼ ';
this.logText(
indentLevel,
`▼ ${this.getCallerInfo()} ${this.separator(text)} ${text}`
logLevelSymbol,
`${this.getFullMessage(callerInfo, text)}`
);
}

Expand All @@ -89,34 +101,43 @@ export abstract class Logger {
) {
return;
}
const callerInfo: string = this.getCallerInfo();
const logLevelSymbol: string = '‣ ';
this.logText(
indentLevel,
`‣ ${this.getCallerInfo()} ${this.separator(text)} ${text}`
logLevelSymbol,
`${this.getFullMessage(callerInfo, text)}`
);
}

private static logText(messageIndentationLevel: number, text: string): void {
// start group for every level
for (let i: number = 0; i < messageIndentationLevel; i++) {
console.group();
}
// print the trimmed text
// if multiline, the message should be properly padded
console.log(text);
// end group for every level
for (let i: number = 0; i < messageIndentationLevel; i++) {
console.groupEnd();
private static getFullMessage(callerInfo: string, text: string): string {
return `${callerInfo}${this.separator(text, callerInfo)}${text}`;
}

private static logText(messageIndentationLevel: number, logLevelSymbol: string, text: string): void {
if (text) {
// start group for every level
for (let i: number = 0; i < messageIndentationLevel; i++) {
console.group();
}
// print the trimmed text
// if multiline, the message should be properly padded
console.log(logLevelSymbol + text);
// end group for every level
for (let i: number = 0; i < messageIndentationLevel; i++) {
console.groupEnd();
}
}
}

private static getCallerInfo(): string {
const e: Error = new Error();
const stack: string[] = e.stack ? e.stack.split('\n') : [];
// " at functionName ( ..." => "functionName"
return stack[3].replace(/^\s+at\s+(.+?)\s.+/g, '$1');
return stack[3].includes('.<anonymous') ? '' : stack[3].replace(/^\s+at\s+(.+?)\s.+/g, '$1');
}

private static separator(text: string): string {
return text ? '-' : '';
private static separator(text: string, caller: string): string {
return text ? caller ? ' - ' : '' : '';
}
}

0 comments on commit 7700995

Please sign in to comment.