Skip to content

Commit d214726

Browse files
author
Antoine Tremblay
committed
Make it possible call the logger outside of the inversify context
This patch exports the logger global variable that points to the resolved logger after application activation. This enables code outside of the inversify context to use that global logger variable to log without needing the injection of a ILogger. Signed-off-by: Antoine Tremblay <[email protected]>
1 parent 340967b commit d214726

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

packages/core/src/browser/logger-frontend-module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
import { ContainerModule, Container } from 'inversify';
99
import { ILoggerServer, loggerPath } from '../common/logger-protocol';
10-
import { ILogger, Logger, LoggerFactory, LoggerOptions } from '../common/logger';
10+
import { ILogger, Logger, LoggerFactory, LoggerOptions, setGlobalLogger } from '../common/logger';
1111
import { LoggerWatcher } from '../common/logger-watcher';
1212
import { WebSocketConnectionProvider } from './messaging';
1313

1414
export const loggerFrontendModule = new ContainerModule(bind => {
15-
bind(ILogger).to(Logger).inSingletonScope();
15+
bind(ILogger).to(Logger).inSingletonScope().whenTargetIsDefault().onActivation((context, alogger: ILogger) => {
16+
setGlobalLogger(alogger);
17+
return alogger;
18+
});
1619
bind(LoggerWatcher).toSelf().inSingletonScope();
1720
bind(ILoggerServer).toDynamicValue(ctx => {
1821
const loggerWatcher = ctx.container.get(LoggerWatcher);

packages/core/src/common/logger.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { inject, injectable, optional } from 'inversify';
99
import { LoggerWatcher } from './logger-watcher';
1010
import { ILoggerServer } from './logger-protocol';
1111

12+
/* This is initialized onActivation it is exported so the logger can be used
13+
outside of the inversify context. */
14+
export let logger: ILogger;
15+
16+
export function setGlobalLogger(alogger: ILogger) {
17+
logger = alogger;
18+
}
19+
1220
export enum LogLevel {
1321
FATAL = 60,
1422
ERROR = 50,
@@ -365,4 +373,4 @@ export class Logger extends AbstractLogger implements ILogger {
365373
child(obj: object): ILogger {
366374
return this.factory(obj);
367375
}
368-
}
376+
}

packages/core/src/node/logger-backend-module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
import { ContainerModule, Container } from 'inversify';
99
import { ConnectionHandler, JsonRpcConnectionHandler } from "../common/messaging";
10-
import { ILogger, LoggerFactory, LoggerOptions, Logger } from '../common/logger';
10+
import { ILogger, LoggerFactory, LoggerOptions, Logger, setGlobalLogger } from '../common/logger';
1111
import { ILoggerServer, ILoggerClient, loggerPath, LoggerServerOptions } from '../common/logger-protocol';
1212
import { BunyanLoggerServer } from './bunyan-logger-server';
1313
import { LoggerWatcher } from '../common/logger-watcher';
1414
import * as yargs from 'yargs';
1515

1616
export const loggerBackendModule = new ContainerModule(bind => {
17-
bind(ILogger).to(Logger).inSingletonScope().whenTargetIsDefault();
17+
bind(ILogger).to(Logger).inSingletonScope().whenTargetIsDefault().onActivation((context, alogger: ILogger) => {
18+
setGlobalLogger(alogger);
19+
return alogger;
20+
});
1821
bind(LoggerWatcher).toSelf().inSingletonScope();
1922
bind(ILoggerServer).to(BunyanLoggerServer).inSingletonScope();
2023
bind(LoggerServerOptions).toDynamicValue(ctx => {

packages/process/src/node/terminal-process.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { injectable, inject } from 'inversify';
99
import * as stream from 'stream';
10-
import { ILogger } from '@theia/core/lib/common';
10+
import { logger } from '@theia/core/lib/common';
1111
import { Process } from './process';
1212
import { ProcessManager } from './process-manager';
1313

@@ -47,11 +47,10 @@ export class TerminalProcess extends Process {
4747

4848
constructor(
4949
@inject(TerminalProcessOptions) options: TerminalProcessOptions,
50-
@inject(ProcessManager) processManager: ProcessManager,
51-
@inject(ILogger) logger: ILogger) {
50+
@inject(ProcessManager) processManager: ProcessManager) {
5251
super(processManager, logger);
5352

54-
this.logger.debug(`Starting terminal process: ${options.command},`
53+
logger.info(`Starting terminal process: ${options.command},`
5554
+ ` with args : ${options.args}, `
5655
+ ` options ${JSON.stringify(options.options)} `);
5756

packages/terminal/src/node/shell-process.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66
*/
77

8-
import { injectable, inject, named } from 'inversify';
8+
import { injectable, inject } from 'inversify';
99
import * as process from 'process';
10-
import { ILogger } from '@theia/core/lib/common/logger';
1110
import { TerminalProcess, TerminalProcessOptions, ProcessManager } from '@theia/process/lib/node';
1211
import { isWindows } from "@theia/core/lib/common";
1312
import URI from "@theia/core/lib/common/uri";
@@ -41,7 +40,6 @@ export class ShellProcess extends TerminalProcess {
4140
constructor(
4241
@inject(ShellProcessOptions) options: ShellProcessOptions,
4342
@inject(ProcessManager) processManager: ProcessManager,
44-
@inject(ILogger) @named("terminal") logger: ILogger
4543
) {
4644
super(<TerminalProcessOptions>{
4745
command: options.shell || ShellProcess.getShellExecutablePath(),
@@ -53,7 +51,7 @@ export class ShellProcess extends TerminalProcess {
5351
cwd: getRootPath(options.rootURI),
5452
env: process.env as any
5553
}
56-
}, processManager, logger);
54+
}, processManager);
5755
}
5856

5957
protected static getShellExecutablePath(): string {

0 commit comments

Comments
 (0)