Improve safety of log service

This commit is contained in:
Daniel Imms
2023-06-16 10:30:54 -07:00
parent f79e9a1951
commit 4d501b38b8
2 changed files with 8 additions and 7 deletions
+7 -6
View File
@@ -33,7 +33,8 @@ const LOG_PREFIX = 'xterm.js: ';
export class LogService extends Disposable implements ILogService { export class LogService extends Disposable implements ILogService {
public serviceBrand: any; public serviceBrand: any;
public logLevel: LogLevelEnum = LogLevelEnum.OFF; private _logLevel: LogLevelEnum = LogLevelEnum.OFF;
public get logLevel(): LogLevelEnum { return this._logLevel; }
constructor( constructor(
@IOptionsService private readonly _optionsService: IOptionsService @IOptionsService private readonly _optionsService: IOptionsService
@@ -44,7 +45,7 @@ export class LogService extends Disposable implements ILogService {
} }
private _updateLogLevel(): void { private _updateLogLevel(): void {
this.logLevel = optionsKeyToLogLevel[this._optionsService.rawOptions.logLevel]; this._logLevel = optionsKeyToLogLevel[this._optionsService.rawOptions.logLevel];
} }
private _evalLazyOptionalParams(optionalParams: any[]): void { private _evalLazyOptionalParams(optionalParams: any[]): void {
@@ -61,25 +62,25 @@ export class LogService extends Disposable implements ILogService {
} }
public debug(message: string, ...optionalParams: any[]): void { public debug(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.DEBUG) { if (this._logLevel <= LogLevelEnum.DEBUG) {
this._log(this._optionsService.options.logger?.debug.bind(this._optionsService.options.logger) ?? console.log, message, optionalParams); this._log(this._optionsService.options.logger?.debug.bind(this._optionsService.options.logger) ?? console.log, message, optionalParams);
} }
} }
public info(message: string, ...optionalParams: any[]): void { public info(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.INFO) { if (this._logLevel <= LogLevelEnum.INFO) {
this._log(this._optionsService.options.logger?.info.bind(this._optionsService.options.logger) ?? console.info, message, optionalParams); this._log(this._optionsService.options.logger?.info.bind(this._optionsService.options.logger) ?? console.info, message, optionalParams);
} }
} }
public warn(message: string, ...optionalParams: any[]): void { public warn(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.WARN) { if (this._logLevel <= LogLevelEnum.WARN) {
this._log(this._optionsService.options.logger?.warn.bind(this._optionsService.options.logger) ?? console.warn, message, optionalParams); this._log(this._optionsService.options.logger?.warn.bind(this._optionsService.options.logger) ?? console.warn, message, optionalParams);
} }
} }
public error(message: string, ...optionalParams: any[]): void { public error(message: string, ...optionalParams: any[]): void {
if (this.logLevel <= LogLevelEnum.ERROR) { if (this._logLevel <= LogLevelEnum.ERROR) {
this._log(this._optionsService.options.logger?.error.bind(this._optionsService.options.logger) ?? console.error, message, optionalParams); this._log(this._optionsService.options.logger?.error.bind(this._optionsService.options.logger) ?? console.error, message, optionalParams);
} }
} }
+1 -1
View File
@@ -157,7 +157,7 @@ export const ILogService = createDecorator<ILogService>('LogService');
export interface ILogService { export interface ILogService {
serviceBrand: undefined; serviceBrand: undefined;
logLevel: LogLevelEnum; readonly logLevel: LogLevelEnum;
debug(message: any, ...optionalParams: any[]): void; debug(message: any, ...optionalParams: any[]): void;
info(message: any, ...optionalParams: any[]): void; info(message: any, ...optionalParams: any[]): void;