Skip to content

Commit c2ca817

Browse files
Ensure ConsoleLogger logs the correct log level of the message
1 parent 86e89a1 commit c2ca817

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

packages/server/src/common/utils/console-logger.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { BindingContext } from '@eclipse-glsp/protocol/lib/di';
1818
import { injectable } from 'inversify';
19-
import { LogLevel, Logger, LoggerConfigOptions, LoggerFactory, NullLogger, getRequestParentName } from './logger';
19+
import { LogLevel, Logger, LoggerConfigOptions, LoggerFactory, NullLogger, asLogLevel, getRequestParentName } from './logger';
2020

2121
/**
2222
* Simple logger implementation that forwards logging calls to the `console` and
@@ -33,48 +33,54 @@ export class ConsoleLogger extends Logger {
3333

3434
info(message: string, ...params: any[]): void {
3535
if (LogLevel.info <= this.logLevel) {
36-
console.info(this.logMessage(message, params));
36+
console.info(this.logMessage(LogLevel.info, message, params));
3737
}
3838
}
3939

4040
warn(message: string, ...params: any[]): void {
4141
if (LogLevel.warn <= this.logLevel) {
42-
console.warn(this.logMessage(message, params));
42+
console.warn(this.logMessage(LogLevel.warn, message, params));
4343
}
4444
}
4545

4646
error(message: string, ...params: any[]): void {
4747
if (LogLevel.error <= this.logLevel) {
48-
console.error(this.logMessage(message, params));
48+
console.error(this.logMessage(LogLevel.error, message, params));
4949
}
5050
}
5151

5252
debug(message: string, ...params: any[]): void {
5353
if (LogLevel.debug <= this.logLevel) {
54-
console.debug(this.logMessage(message, params));
54+
console.debug(this.logMessage(LogLevel.debug, message, params));
5555
}
5656
}
5757

58-
logMessage(message: string, ...params: any[]): string {
59-
const timestamp = new Date().toLocaleTimeString();
58+
logMessage(level: LogLevel, message: string, ...params: any[]): string {
6059
const caller = this.caller ? `[${this.caller}]` : '';
6160
const additional = this.logAdditionals(...params);
62-
return `${timestamp} ${this.logLevel} - ${caller} ${message} ${additional}`;
61+
return `${this.timestamp()} ${asLogLevel(level)} - ${caller} ${message} ${additional}`;
6362
}
6463

65-
logAdditionals(...params: any[]): string {
66-
if (!params || params.length === 0) {
67-
return '';
68-
}
69-
return params.map(param => this.stringify(param)).join(',\n');
64+
protected timestamp(): string {
65+
return new Date().toLocaleTimeString();
66+
}
67+
68+
logAdditionals(...additionals: any[]): string {
69+
const params = additionals.length === 1 && Array.isArray(additionals[0]) ? additionals[0] : additionals;
70+
return !params || params.length === 0 ? '' : params.map(param => this.stringify(param)).join(this.additionalsSeparator());
71+
}
72+
73+
protected additionalsSeparator(): string {
74+
return ',\n';
7075
}
7176

7277
stringify(param: unknown): string {
73-
if (param instanceof Error) {
74-
return `${param.message}
75-
${param.stack || ''}`;
76-
}
77-
return JSON.stringify(param, undefined, 4);
78+
return param instanceof Error ? this.stringifyError(param) : JSON.stringify(param, undefined, 4);
79+
}
80+
81+
protected stringifyError(error: Error): string {
82+
return `${error.message}
83+
${error.stack || ''}`;
7884
}
7985
}
8086

0 commit comments

Comments
 (0)