Skip to content

Commit d8f9bdc

Browse files
committed
Fixed error logging. Added Warning TraceLevel
1 parent 37dab02 commit d8f9bdc

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

src/util/configuration/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LineNumberFrequency } from '../lineNumberer';
1212
export enum TraceLevel {
1313
Silent = 'silent',
1414
Errors = 'errors',
15+
Warnings = 'warnings',
1516
Verbose = 'verbose',
1617
Debug = 'debug',
1718
}

src/util/logger.ts

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { configuration } from './configuration/config';
99
import { defaults, TraceLevel } from './configuration/defaults';
1010
import { constants } from './constants';
1111

12+
/* Log Levels
13+
Silent - No Output
14+
Errors - Errors only
15+
Warnings - Warnings + Errors
16+
Verbose - Standard Outputs + Warnings + Errors
17+
Debug - Everything
18+
*/
19+
1220
const configTraceLevel = 'general.outputLevel';
1321

1422
export class Logger {
@@ -53,7 +61,12 @@ export class Logger {
5361
}
5462

5563
static debug(message: string): void {
56-
if (this.level !== TraceLevel.Debug) {
64+
if (
65+
this.level === TraceLevel.Silent ||
66+
this.level === TraceLevel.Errors ||
67+
this.level === TraceLevel.Warnings ||
68+
this.level === TraceLevel.Verbose
69+
) {
5770
return;
5871
}
5972

@@ -62,39 +75,50 @@ export class Logger {
6275
}
6376
}
6477

65-
static error(err: Error, message?: string, handled = false): void {
66-
console.error(`${handled ? 'H' : 'Unh'}andled Error: ${err.stack || err.message || err.toString()}`);
67-
68-
if (this.level === TraceLevel.Silent) {
69-
return;
70-
}
71-
72-
if (!err) {
78+
static log(message: string): void {
79+
if (
80+
this.level === TraceLevel.Silent ||
81+
this.level === TraceLevel.Errors ||
82+
this.level === TraceLevel.Warnings
83+
) {
7384
return;
7485
}
7586

7687
if (this.output !== undefined) {
77-
this.output.appendLine(
78-
`${handled ? 'H' : 'Unh'}andled Error: ${err.stack || err.message || err.toString()}`,
79-
);
88+
this.output.appendLine(`${this.level === TraceLevel.Debug ? this.timestamp : ''} ${message}`);
8089
}
8190
}
8291

83-
static log(message: string): void {
84-
if (this.level === TraceLevel.Verbose || this.level === TraceLevel.Debug) {
85-
if (this.output !== undefined) {
86-
this.output.appendLine(`${this.level === TraceLevel.Debug ? this.timestamp : ''} ${message}`);
87-
}
92+
static warn(message: string): void {
93+
if (this.level === TraceLevel.Silent || this.level === TraceLevel.Errors) {
94+
return;
95+
}
96+
97+
if (this.output !== undefined) {
98+
this.output.appendLine(message);
8899
}
89100
}
90101

91-
static warn(message: string): void {
102+
static error(err: Error | unknown, message?: string): void {
92103
if (this.level === TraceLevel.Silent) {
93104
return;
94105
}
95106

107+
if (message == null) {
108+
const stack = err instanceof Error ? err.stack : undefined;
109+
110+
if (stack) {
111+
const match = /.*\s*?at\s(.+?)\s/.exec(stack);
112+
if (match != null) {
113+
message = match[1];
114+
}
115+
}
116+
}
117+
96118
if (this.output !== undefined) {
97-
this.output.appendLine(message);
119+
this.output.appendLine(
120+
`${this.level === TraceLevel.Debug ? this.timestamp : ''} ${message ?? ''}\n${String(err)}`,
121+
);
98122
}
99123
}
100124

0 commit comments

Comments
 (0)