Skip to content

Commit 867d5a7

Browse files
Ensure ConsoleLogger logs the correct log level of the message (#106)
1 parent 86e89a1 commit 867d5a7

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

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

Lines changed: 26 additions & 19 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,55 @@ 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();
60-
const caller = this.caller ? `[${this.caller}]` : '';
61-
const additional = this.logAdditionals(...params);
62-
return `${timestamp} ${this.logLevel} - ${caller} ${message} ${additional}`;
58+
logMessage(level: LogLevel, message: string, ...params: any[]): string {
59+
return `${this.logTimestamp()} ${asLogLevel(level)} - ${this.logCaller()} ${message} ${this.logAdditionals(...params)}`;
60+
}
61+
62+
protected logCaller(): string {
63+
return this.caller ? `[${this.caller}]` : '';
64+
}
65+
66+
protected logTimestamp(): string {
67+
return new Date().toLocaleTimeString();
6368
}
6469

6570
logAdditionals(...params: any[]): string {
66-
if (!params || params.length === 0) {
67-
return '';
68-
}
69-
return params.map(param => this.stringify(param)).join(',\n');
71+
return !params || params.length === 0 ? '' : params.map(param => this.stringify(param)).join(this.logAdditionalsSeparator());
72+
}
73+
74+
protected logAdditionalsSeparator(): string {
75+
return ',\n';
7076
}
7177

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

0 commit comments

Comments
 (0)