Skip to content

Commit 2c45989

Browse files
authored
Merge pull request #4563 from Tyriar/logger
Add logger delegate option
2 parents ac923e5 + f779e4d commit 2c45989

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

src/common/services/LogService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,30 @@ export class LogService extends Disposable implements ILogService {
5757

5858
private _log(type: LogType, message: string, optionalParams: any[]): void {
5959
this._evalLazyOptionalParams(optionalParams);
60-
type.call(console, LOG_PREFIX + message, ...optionalParams);
60+
type.call(console, (this._optionsService.options.logger ? '' : LOG_PREFIX) + message, ...optionalParams);
6161
}
6262

6363
public debug(message: string, ...optionalParams: any[]): void {
6464
if (this.logLevel <= LogLevelEnum.DEBUG) {
65-
this._log(console.log, message, optionalParams);
65+
this._log(this._optionsService.options.logger?.debug ?? console.log, message, optionalParams);
6666
}
6767
}
6868

6969
public info(message: string, ...optionalParams: any[]): void {
7070
if (this.logLevel <= LogLevelEnum.INFO) {
71-
this._log(console.info, message, optionalParams);
71+
this._log(this._optionsService.options.logger?.info ?? console.info, message, optionalParams);
7272
}
7373
}
7474

7575
public warn(message: string, ...optionalParams: any[]): void {
7676
if (this.logLevel <= LogLevelEnum.WARN) {
77-
this._log(console.warn, message, optionalParams);
77+
this._log(this._optionsService.options.logger?.warn ?? console.warn, message, optionalParams);
7878
}
7979
}
8080

8181
public error(message: string, ...optionalParams: any[]): void {
8282
if (this.logLevel <= LogLevelEnum.ERROR) {
83-
this._log(console.error, message, optionalParams);
83+
this._log(this._optionsService.options.logger?.error ?? console.error, message, optionalParams);
8484
}
8585
}
8686
}

src/common/services/OptionsService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = {
2727
letterSpacing: 0,
2828
linkHandler: null,
2929
logLevel: 'info',
30+
logger: null,
3031
scrollback: 1000,
3132
scrollOnUserInput: true,
3233
scrollSensitivity: 1,

src/common/services/Services.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IEvent, IEventEmitter } from 'common/EventEmitter';
77
import { IBuffer, IBufferSet } from 'common/buffer/Types';
88
import { IDecPrivateModes, ICoreMouseEvent, CoreMouseEncoding, ICoreMouseProtocol, CoreMouseEventType, ICharset, IWindowOptions, IModes, IAttributeData, ScrollSource, IDisposable, IColor, CursorStyle, IOscLinkData } from 'common/Types';
99
import { createDecorator } from 'common/services/ServiceRegistry';
10-
import { IDecorationOptions, IDecoration, ILinkHandler, IWindowsPty } from 'xterm';
10+
import { IDecorationOptions, IDecoration, ILinkHandler, IWindowsPty, ILogger } from 'xterm';
1111

1212
export const IBufferService = createDecorator<IBufferService>('BufferService');
1313
export interface IBufferService {
@@ -229,6 +229,7 @@ export interface ITerminalOptions {
229229
lineHeight?: number;
230230
linkHandler?: ILinkHandler | null;
231231
logLevel?: LogLevel;
232+
logger?: ILogger | null;
232233
macOptionIsMeta?: boolean;
233234
macOptionClickForcesSelection?: boolean;
234235
minimumContrastRatio?: number;

typings/xterm-headless.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ declare module 'xterm-headless' {
107107
*/
108108
logLevel?: LogLevel;
109109

110+
/**
111+
* A logger to use instead of `console`.
112+
*/
113+
logger?: ILogger | null;
114+
110115
/**
111116
* Whether to treat option as the meta key.
112117
*/
@@ -304,6 +309,32 @@ declare module 'xterm-headless' {
304309
buildNumber?: number;
305310
}
306311

312+
/**
313+
* A replacement logger for `console`.
314+
*/
315+
export interface ILogger {
316+
/**
317+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
318+
* debug.
319+
*/
320+
debug(message: string, ...args: any[]): void;
321+
/**
322+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
323+
* info or below.
324+
*/
325+
info(message: string, ...args: any[]): void;
326+
/**
327+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
328+
* warn or below.
329+
*/
330+
warn(message: string, ...args: any[]): void;
331+
/**
332+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
333+
* error or below.
334+
*/
335+
error(message: string | Error, ...args: any[]): void;
336+
}
337+
307338
/**
308339
* An object that can be disposed via a dispose function.
309340
*/

typings/xterm.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ declare module 'xterm' {
152152
*/
153153
logLevel?: LogLevel;
154154

155+
/**
156+
* A logger to use instead of `console`.
157+
*/
158+
logger?: ILogger | null;
159+
155160
/**
156161
* Whether to treat option as the meta key.
157162
*/
@@ -365,6 +370,32 @@ declare module 'xterm' {
365370
buildNumber?: number;
366371
}
367372

373+
/**
374+
* A replacement logger for `console`.
375+
*/
376+
export interface ILogger {
377+
/**
378+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
379+
* debug.
380+
*/
381+
debug(message: string, ...args: any[]): void;
382+
/**
383+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
384+
* info or below.
385+
*/
386+
info(message: string, ...args: any[]): void;
387+
/**
388+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
389+
* warn or below.
390+
*/
391+
warn(message: string, ...args: any[]): void;
392+
/**
393+
* Log a debug message, this will only be called if {@link ITerminalOptions.logLevel} is set to
394+
* error or below.
395+
*/
396+
error(message: string | Error, ...args: any[]): void;
397+
}
398+
368399
/**
369400
* An object that can be disposed via a dispose function.
370401
*/

0 commit comments

Comments
 (0)