diff --git a/packages/bull/src/framework.ts b/packages/bull/src/framework.ts index 29b0b23bbf2..a46fe59f59b 100644 --- a/packages/bull/src/framework.ts +++ b/packages/bull/src/framework.ts @@ -44,6 +44,7 @@ export class BullFramework private bullDefaultConcurrency: number; private bullClearRepeatJobWhenStart: boolean; private queueMap: Map = new Map(); + protected frameworkLoggerName = 'bullLogger'; @Logger('bullLogger') protected bullLogger: ILogger; @@ -72,10 +73,6 @@ export class BullFramework return 'bull'; } - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('bullLogger'); - } - async run() { const processorModules = DecoratorManager.listModule(BULL_PROCESSOR_KEY); for (const mod of processorModules) { diff --git a/packages/core/src/baseFramework.ts b/packages/core/src/baseFramework.ts index 203ac7684ac..6c878d3d96e 100644 --- a/packages/core/src/baseFramework.ts +++ b/packages/core/src/baseFramework.ts @@ -48,7 +48,7 @@ export abstract class BaseFramework< public app: APP; public configurationOptions: OPT; protected logger: ILogger; - protected appLogger: ILogger; + protected frameworkLoggerName = 'appLogger'; protected defaultContext = {}; protected middlewareManager = this.createMiddlewareManager(); protected filterManager = this.createFilterManager(); @@ -82,7 +82,6 @@ export abstract class BaseFramework< protected async init() { this.configurationOptions = this.configure() ?? ({} as OPT); this.logger = this.loggerService.getLogger('coreLogger'); - this.appLogger = this.loggerService.getLogger('appLogger'); return this; } @@ -91,7 +90,6 @@ export abstract class BaseFramework< options: IMidwayBootstrapOptions ): void | Promise; public abstract run(): Promise; - public abstract getFrameworkLogger(): ILogger; public isEnable(): boolean { return true; @@ -129,7 +127,7 @@ export abstract class BaseFramework< } protected createContextLogger(ctx: CTX, name?: string): ILogger { - if (name && name !== 'appLogger') { + if (name && name !== this.frameworkLoggerName) { const appLogger = this.getLogger(name); let ctxLoggerCache = ctx.getAttr(REQUEST_CTX_LOGGER_CACHE_KEY) as Map< string, @@ -149,7 +147,7 @@ export abstract class BaseFramework< ctxLoggerCache.set(name, ctxLogger); return ctxLogger; } else { - const appLogger = name ? this.getLogger(name) : this.getFrameworkLogger(); + const appLogger = this.getLogger(name); // avoid maximum call stack size exceeded if (ctx['_logger']) { return ctx['_logger']; @@ -371,7 +369,7 @@ export abstract class BaseFramework< } public getLogger(name?: string) { - return this.loggerService.getLogger(name) ?? this.appLogger; + return this.loggerService.getLogger(name ?? this.frameworkLoggerName); } public getCoreLogger() { @@ -435,4 +433,12 @@ export abstract class BaseFramework< public getNamespace() { return this.namespace; } + + /** + * Set the default framework logger name + * @since 4.0.0 + */ + public setFrameworkLoggerName(loggerName: string) { + this.frameworkLoggerName = loggerName; + } } diff --git a/packages/core/src/interface.ts b/packages/core/src/interface.ts index 4f9d9097e25..a447e1bd0ed 100644 --- a/packages/core/src/interface.ts +++ b/packages/core/src/interface.ts @@ -1078,7 +1078,6 @@ export interface IMidwayFramework< getBaseDir(): string; getLogger(name?: string): ILogger; getCoreLogger(): ILogger; - getFrameworkLogger(): ILogger; createLogger(name: string, options: MidwayLoggerOptions): ILogger; getProjectName(): string; useMiddleware(Middleware: CommonMiddlewareUnion): void; @@ -1090,6 +1089,7 @@ export interface IMidwayFramework< useGuard(guard: CommonGuardUnion): void; runGuard(ctx: CTX, supplierClz: new (...args) => any, methodName: string): Promise; getNamespace(): string; + setFrameworkLoggerName(name: string): void; } export interface MidwayAppInfo { diff --git a/packages/core/test/baseFramework.test.ts b/packages/core/test/baseFramework.test.ts index a572a6c952b..d2ad0d51296 100644 --- a/packages/core/test/baseFramework.test.ts +++ b/packages/core/test/baseFramework.test.ts @@ -859,7 +859,7 @@ describe('/test/baseFramework.test.ts', () => { const mainFramework = midwayFrameworkService.getMainFramework(); const ctx = mainFramework.getApplication().createAnonymousContext(); expect(ctx.logger.info('hello world')).toEqual('[custom ctx] hello world'); - expect(ctx.getLogger('appLogger').info('hello world')).toEqual('[custom ctx] hello world'); + expect(ctx.getLogger('customFrameworkLogger').info('hello world')).toEqual('[custom ctx] hello world'); expect(ctx.getLogger('customLogger').info('hello world')).toEqual('[new custom ctx] hello world'); expect(mainFramework.getLogger('customLogger').info('hello world')).toEqual('[new custom] hello world'); }); diff --git a/packages/core/test/common/applicationManager.test.ts b/packages/core/test/common/applicationManager.test.ts index 0f0f93fa642..1d6d5a01d20 100644 --- a/packages/core/test/common/applicationManager.test.ts +++ b/packages/core/test/common/applicationManager.test.ts @@ -1,5 +1,4 @@ import { MidwayApplicationManager, BaseFramework, MidwayMockService } from '../../src'; -import { ILogger } from '@midwayjs/logger'; describe('test/common/applicationManager.test.ts', () => { it('should test application manager', async () => { @@ -18,10 +17,6 @@ describe('test/common/applicationManager.test.ts', () => { run(): Promise { return Promise.resolve(undefined); } - - getFrameworkLogger(): ILogger { - return undefined; - } } const framework = new CustomFramework1({} as any); diff --git a/packages/core/test/fixtures/base-app-custom-framework-logger/src/framework.ts b/packages/core/test/fixtures/base-app-custom-framework-logger/src/framework.ts index ff8f0629d54..6894ebebd0e 100644 --- a/packages/core/test/fixtures/base-app-custom-framework-logger/src/framework.ts +++ b/packages/core/test/fixtures/base-app-custom-framework-logger/src/framework.ts @@ -1,8 +1,8 @@ import { BaseFramework, Framework, IMidwayBootstrapOptions } from '../../../../src'; -import { ILogger } from '@midwayjs/logger'; @Framework() export class CustomFramework extends BaseFramework { + frameworkLoggerName = 'customFrameworkLogger'; async applicationInitialize(options: IMidwayBootstrapOptions) { this.app = {}; } @@ -12,11 +12,7 @@ export class CustomFramework extends BaseFramework { } run(): Promise { - this.app.getCoreLogger().info('run custom framework'); + this.logger.info('run custom framework'); return Promise.resolve(undefined); } - - getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('customFrameworkLogger'); - } } diff --git a/packages/core/test/fixtures/base-app-multi-framework-shared/src/framework.ts b/packages/core/test/fixtures/base-app-multi-framework-shared/src/framework.ts index 43605223f26..739e74f9046 100644 --- a/packages/core/test/fixtures/base-app-multi-framework-shared/src/framework.ts +++ b/packages/core/test/fixtures/base-app-multi-framework-shared/src/framework.ts @@ -4,7 +4,6 @@ import { IMidwayApplication, IMidwayBootstrapOptions, } from '../../../../src'; -import { ILogger } from '@midwayjs/logger'; @Framework() export class LightFramework extends BaseFramework { @@ -23,8 +22,4 @@ export class LightFramework extends BaseFramework { getFrameworkName(): string { return 'grpc'; } - - getFrameworkLogger(): ILogger { - return undefined; - } } diff --git a/packages/core/test/setup.test.ts b/packages/core/test/setup.test.ts index 89b13cbec6b..9d3927475cd 100644 --- a/packages/core/test/setup.test.ts +++ b/packages/core/test/setup.test.ts @@ -10,7 +10,6 @@ import { Framework, Inject, } from '../src'; -import { ILogger } from '@midwayjs/logger'; describe('/test/setup.test.ts', () => { it('should test setup and config', async () => { @@ -140,10 +139,6 @@ describe('/test/setup.test.ts', () => { configure() { return {}; } - - getFrameworkLogger(): ILogger { - return undefined; - } } @Configuration() diff --git a/packages/core/test/util.ts b/packages/core/test/util.ts index 1469959ee83..8e64ec7fb5c 100644 --- a/packages/core/test/util.ts +++ b/packages/core/test/util.ts @@ -22,7 +22,6 @@ import { import { join } from 'path'; import * as http from 'http'; import * as getRawBody from 'raw-body'; -import { ILogger } from '@midwayjs/logger'; /** * 任意一个数组中的对象,和预期的对象属性一致即可 @@ -129,10 +128,6 @@ export async function createLightFramework(baseDir: string = '', bootstrapOption getFrameworkName(): string { return 'light'; } - - getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } const conf = { diff --git a/packages/cron/src/framework.ts b/packages/cron/src/framework.ts index c2ce103071f..ce7bd4c09c4 100644 --- a/packages/cron/src/framework.ts +++ b/packages/cron/src/framework.ts @@ -7,7 +7,6 @@ import { MidwayInvokeForbiddenError, Utils, MetadataManager, - ILogger, } from '@midwayjs/core'; import { Application, @@ -23,6 +22,7 @@ import { CRON_JOB_KEY } from './constants'; export class CronFramework extends BaseFramework { private defaultCronJobConfig: CronOptions; private jobs: Map = new Map(); + protected frameworkLoggerName = 'cronLogger'; async applicationInitialize(options: IMidwayBootstrapOptions) { this.app = {} as any; @@ -42,10 +42,6 @@ export class CronFramework extends BaseFramework { return 'cron'; } - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('cronLogger'); - } - async run() { const jobModules = DecoratorManager.listModule(CRON_JOB_KEY); for (const mod of jobModules) { diff --git a/packages/faas/src/framework.ts b/packages/faas/src/framework.ts index 8c5ecba3e7a..fcb03abc05b 100644 --- a/packages/faas/src/framework.ts +++ b/packages/faas/src/framework.ts @@ -26,7 +26,6 @@ import { WEB_RESPONSE_REDIRECT, httpError, MidwayFeatureNotImplementedError, - ILogger, } from '@midwayjs/core'; import SimpleLock from '@midwayjs/simple-lock'; import { createConsoleLogger, LoggerOptions, loggers } from '@midwayjs/logger'; @@ -593,8 +592,4 @@ export class MidwayFaaSFramework extends BaseFramework< public getAllHandlerNames() { return Array.from(this.funMappingStore.keys()); } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/grpc/src/provider/framework.ts b/packages/grpc/src/provider/framework.ts index e87ca99f958..1d6e782abcd 100644 --- a/packages/grpc/src/provider/framework.ts +++ b/packages/grpc/src/provider/framework.ts @@ -22,7 +22,6 @@ import { MidwayInvokeForbiddenError, DecoratorManager, MetadataManager, - ILogger, } from '@midwayjs/core'; import { Context, @@ -275,8 +274,4 @@ export class MidwayGRPCFramework extends BaseFramework< public getFrameworkName() { return 'gRPC'; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/kafka/src/framework.ts b/packages/kafka/src/framework.ts index a2db1053aa9..296520e7e84 100644 --- a/packages/kafka/src/framework.ts +++ b/packages/kafka/src/framework.ts @@ -52,6 +52,8 @@ export class MidwayKafkaFramework extends BaseFramework< IKafkaConsumerInitOptions, IKafkaConsumer >; + protected frameworkLoggerName = 'kafkaLogger'; + configure() { return this.configService.getConfiguration('kafka'); } @@ -316,8 +318,4 @@ export class MidwayKafkaFramework extends BaseFramework< await this.typedResourceManager.destroy(); } } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('kafkaLogger'); - } } diff --git a/packages/mock/src/creator.ts b/packages/mock/src/creator.ts index 4eb2169e325..60274dd45c6 100644 --- a/packages/mock/src/creator.ts +++ b/packages/mock/src/creator.ts @@ -18,7 +18,6 @@ import { ObjectIdentifier, isTypeScriptEnvironment, DecoratorManager, - ILogger, } from '@midwayjs/core'; import { isAbsolute, join, resolve } from 'path'; import { clearAllLoggers, loggers } from '@midwayjs/logger'; @@ -602,10 +601,6 @@ class LightFramework extends BaseFramework { getFrameworkName(): string { return 'lightFramework'; } - - getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } class BootstrapAppStarter implements IBootstrapAppStarter { diff --git a/packages/mqtt/src/framework.ts b/packages/mqtt/src/framework.ts index 92f743250d5..bcce870cd1f 100644 --- a/packages/mqtt/src/framework.ts +++ b/packages/mqtt/src/framework.ts @@ -1,10 +1,8 @@ import { Framework, BaseFramework, - Logger, DecoratorManager, MetadataManager, - ILogger, } from '@midwayjs/core'; import { IMidwayMQTTApplication, @@ -24,9 +22,7 @@ export class MidwayMQTTFramework extends BaseFramework< > { public app: IMidwayMQTTApplication; protected subscriberMap: Map = new Map(); - - @Logger('mqttLogger') - mqttLogger: ILogger; + protected frameworkLoggerName = 'mqttLogger'; configure() { return this.configService.getConfiguration('mqtt'); @@ -40,7 +36,7 @@ export class MidwayMQTTFramework extends BaseFramework< const { sub } = this.configurationOptions; if (Object.keys(sub || {}).length === 0) { - this.mqttLogger.info( + this.logger.info( '[midway-mqtt] Not found consumer config, skip init consumer' ); } @@ -69,7 +65,7 @@ export class MidwayMQTTFramework extends BaseFramework< protected async beforeStop(): Promise { for (const [name, consumer] of this.subscriberMap) { await consumer.endAsync(); - this.mqttLogger.info(`[midway-mqtt] subscriber: ${name} is closed`); + this.logger.info(`[midway-mqtt] subscriber: ${name} is closed`); } } @@ -107,7 +103,7 @@ export class MidwayMQTTFramework extends BaseFramework< resolve(client); }); client.on('error', err => { - this.mqttLogger.error(err); + this.logger.error(err); }); }); @@ -143,8 +139,4 @@ export class MidwayMQTTFramework extends BaseFramework< public getFrameworkName() { return 'mqtt'; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('mqttLogger'); - } } diff --git a/packages/rabbitmq/src/framework.ts b/packages/rabbitmq/src/framework.ts index d5f27ebff2c..4d1f92a742a 100644 --- a/packages/rabbitmq/src/framework.ts +++ b/packages/rabbitmq/src/framework.ts @@ -9,7 +9,6 @@ import { DecoratorManager, MetadataManager, listPropertyDataFromClass, - ILogger, } from '@midwayjs/core'; import { IMidwayRabbitMQApplication, @@ -121,8 +120,4 @@ export class MidwayRabbitMQFramework extends BaseFramework< public getFrameworkName() { return 'rabbitmq'; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/socketio/src/framework.ts b/packages/socketio/src/framework.ts index 7ecca35e760..09e9aa320b0 100644 --- a/packages/socketio/src/framework.ts +++ b/packages/socketio/src/framework.ts @@ -24,7 +24,6 @@ import { WSEventInfo, WSEventTypeEnum, Framework, - ILogger, } from '@midwayjs/core'; @Framework() @@ -345,8 +344,4 @@ export class MidwaySocketIOFramework extends BaseFramework< > { return this.connectionMiddlewareManager; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/web-express/src/framework.ts b/packages/web-express/src/framework.ts index 5397839f401..d4444fea3a0 100644 --- a/packages/web-express/src/framework.ts +++ b/packages/web-express/src/framework.ts @@ -14,7 +14,6 @@ import { WEB_RESPONSE_HEADER, WEB_RESPONSE_HTTP_CODE, WEB_RESPONSE_REDIRECT, - ILogger, } from '@midwayjs/core'; import { IMidwayExpressApplication, @@ -394,8 +393,4 @@ export class MidwayExpressFramework extends BaseFramework< public getFrameworkName() { return 'express'; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/web-koa/src/framework.ts b/packages/web-koa/src/framework.ts index fb278b9da01..22bf3c5a69c 100644 --- a/packages/web-koa/src/framework.ts +++ b/packages/web-koa/src/framework.ts @@ -12,7 +12,6 @@ import { MidwayWebRouterService, Framework, Types, - ILogger, } from '@midwayjs/core'; import { Cookies } from '@midwayjs/cookies'; import { @@ -352,8 +351,4 @@ export class MidwayKoaFramework extends BaseFramework< ) { this.filterManager.useFilter(Filter); } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/web/src/framework/web.ts b/packages/web/src/framework/web.ts index 78b629df7ea..54f45c8b6b5 100644 --- a/packages/web/src/framework/web.ts +++ b/packages/web/src/framework/web.ts @@ -10,7 +10,6 @@ import { WebControllerGenerator, Framework, Inject, - ILogger, } from '@midwayjs/core'; import { IMidwayWebConfigurationOptions, @@ -292,7 +291,7 @@ export class MidwayWebFramework extends BaseFramework< if (name) { return this.app.loggers[name] || loggers.getLogger(name); } - return this.appLogger; + return this.loggerService.getLogger(this.frameworkLoggerName); } public setContextLoggerClass() { @@ -330,8 +329,4 @@ export class MidwayWebFramework extends BaseFramework< public setServer(server) { this.server = server; } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('appLogger'); - } } diff --git a/packages/ws/src/framework.ts b/packages/ws/src/framework.ts index 91507bb5a1e..6469074f97d 100644 --- a/packages/ws/src/framework.ts +++ b/packages/ws/src/framework.ts @@ -14,7 +14,6 @@ import { MidwayInvokeForbiddenError, DecoratorManager, MetadataManager, - ILogger, } from '@midwayjs/core'; import * as http from 'http'; import { debuglog } from 'util'; @@ -39,6 +38,7 @@ export class MidwayWSFramework extends BaseFramework< server: http.Server; protected heartBeatInterval: NodeJS.Timeout; protected connectionMiddlewareManager = this.createMiddlewareManager(); + protected frameworkLoggerName = 'wsLogger'; configure(): IMidwayWSConfigurationOptions { return this.configService.getConfiguration('webSocket'); @@ -352,10 +352,6 @@ export class MidwayWSFramework extends BaseFramework< }); }, this.configurationOptions.serverHeartbeatInterval); } - - public getFrameworkLogger(): ILogger { - return this.loggerService.getLogger('wsLogger'); - } } function formatResult(result) {