Skip to content

Commit ec636f2

Browse files
committed
Improve winston configuration
Avoid unnecessary instacqntation of the winston logger if logging is disabled via options (noConsoleLog and noFilelog are true)
1 parent 867d5a7 commit ec636f2

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

packages/server/src/node/di/app-module.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import { BindingContext } from '@eclipse-glsp/protocol/lib/di';
1717
import { ContainerModule } from 'inversify';
1818
import * as winston from 'winston';
19-
import { InjectionContainer, LogLevel, Logger, LoggerFactory, getRequestParentName } from '../../common';
19+
import { InjectionContainer, LogLevel, Logger, LoggerFactory, NullLogger, getRequestParentName } from '../../common';
2020
import { LaunchOptions } from '../launch/cli-parser';
2121
import { WinstonLogger } from './winston-logger';
2222

@@ -74,6 +74,18 @@ export function configureWinstonLogger<T extends LaunchOptions>(
7474
rebind = true,
7575
baseLoggerCreator: (launchOptions: T) => winston.Logger = createWinstonInstance
7676
): void {
77+
context.bind(LoggerFactory).toFactory(dynamicContext => (caller: string) => {
78+
const logger = dynamicContext.container.get(Logger);
79+
logger.caller = caller;
80+
return logger;
81+
});
82+
83+
// if no logging is enabled, bind the NullLogge
84+
if (!options.consoleLog && !options.fileLog) {
85+
context.bind(Logger).to(NullLogger).inSingletonScope();
86+
return;
87+
}
88+
7789
const baseLogger = baseLoggerCreator(options);
7890
if (rebind) {
7991
if (context.isBound(Logger)) {
@@ -85,9 +97,4 @@ export function configureWinstonLogger<T extends LaunchOptions>(
8597
}
8698

8799
context.bind(Logger).toDynamicValue(dynamicContext => new WinstonLogger(baseLogger, getRequestParentName(dynamicContext)));
88-
context.bind(LoggerFactory).toFactory(dynamicContext => (caller: string) => {
89-
const logger = dynamicContext.container.get(Logger);
90-
logger.caller = caller;
91-
return logger;
92-
});
93100
}

0 commit comments

Comments
 (0)