Skip to content

Commit

Permalink
fix: framework logger name
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Dec 21, 2024
1 parent 49ddd61 commit 821ea82
Show file tree
Hide file tree
Showing 21 changed files with 26 additions and 105 deletions.
5 changes: 1 addition & 4 deletions packages/bull/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class BullFramework
private bullDefaultConcurrency: number;
private bullClearRepeatJobWhenStart: boolean;
private queueMap: Map<string, BullQueue> = new Map();
protected frameworkLoggerName = 'bullLogger';

@Logger('bullLogger')
protected bullLogger: ILogger;
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/baseFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand All @@ -91,7 +90,6 @@ export abstract class BaseFramework<
options: IMidwayBootstrapOptions
): void | Promise<void>;
public abstract run(): Promise<void>;
public abstract getFrameworkLogger(): ILogger;

public isEnable(): boolean {
return true;
Expand Down Expand Up @@ -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,
Expand All @@ -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'];
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CTX, ResOrNext, Next>): void;
Expand All @@ -1090,6 +1089,7 @@ export interface IMidwayFramework<
useGuard(guard: CommonGuardUnion<CTX>): void;
runGuard(ctx: CTX, supplierClz: new (...args) => any, methodName: string): Promise<boolean>;
getNamespace(): string;
setFrameworkLoggerName(name: string): void;
}

export interface MidwayAppInfo {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
5 changes: 0 additions & 5 deletions packages/core/test/common/applicationManager.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -18,10 +17,6 @@ describe('test/common/applicationManager.test.ts', () => {
run(): Promise<void> {
return Promise.resolve(undefined);
}

getFrameworkLogger(): ILogger {
return undefined;
}
}

const framework = new CustomFramework1({} as any);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseFramework, Framework, IMidwayBootstrapOptions } from '../../../../src';
import { ILogger } from '@midwayjs/logger';

@Framework()
export class CustomFramework extends BaseFramework<any, any, any> {
frameworkLoggerName = 'customFrameworkLogger';
async applicationInitialize(options: IMidwayBootstrapOptions) {
this.app = {};
}
Expand All @@ -12,11 +12,7 @@ export class CustomFramework extends BaseFramework<any, any, any> {
}

run(): Promise<void> {
this.app.getCoreLogger().info('run custom framework');
this.logger.info('run custom framework');
return Promise.resolve(undefined);
}

getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('customFrameworkLogger');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
IMidwayApplication,
IMidwayBootstrapOptions,
} from '../../../../src';
import { ILogger } from '@midwayjs/logger';

@Framework()
export class LightFramework extends BaseFramework<any, any, any> {
Expand All @@ -23,8 +22,4 @@ export class LightFramework extends BaseFramework<any, any, any> {
getFrameworkName(): string {
return 'grpc';
}

getFrameworkLogger(): ILogger {
return undefined;
}
}
5 changes: 0 additions & 5 deletions packages/core/test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -140,10 +139,6 @@ describe('/test/setup.test.ts', () => {
configure() {
return {};
}

getFrameworkLogger(): ILogger {
return undefined;
}
}

@Configuration()
Expand Down
5 changes: 0 additions & 5 deletions packages/core/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
* 任意一个数组中的对象,和预期的对象属性一致即可
Expand Down Expand Up @@ -129,10 +128,6 @@ export async function createLightFramework(baseDir: string = '', bootstrapOption
getFrameworkName(): string {
return 'light';
}

getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('appLogger');
}
}

const conf = {
Expand Down
6 changes: 1 addition & 5 deletions packages/cron/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
MidwayInvokeForbiddenError,
Utils,
MetadataManager,
ILogger,
} from '@midwayjs/core';
import {
Application,
Expand All @@ -23,6 +22,7 @@ import { CRON_JOB_KEY } from './constants';
export class CronFramework extends BaseFramework<Application, Context, any> {
private defaultCronJobConfig: CronOptions;
private jobs: Map<string, CronJob> = new Map();
protected frameworkLoggerName = 'cronLogger';

async applicationInitialize(options: IMidwayBootstrapOptions) {
this.app = {} as any;
Expand All @@ -42,10 +42,6 @@ export class CronFramework extends BaseFramework<Application, Context, any> {
return 'cron';
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('cronLogger');
}

async run() {
const jobModules = DecoratorManager.listModule(CRON_JOB_KEY);
for (const mod of jobModules) {
Expand Down
5 changes: 0 additions & 5 deletions packages/faas/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
}
}
5 changes: 0 additions & 5 deletions packages/grpc/src/provider/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
MidwayInvokeForbiddenError,
DecoratorManager,
MetadataManager,
ILogger,
} from '@midwayjs/core';
import {
Context,
Expand Down Expand Up @@ -275,8 +274,4 @@ export class MidwayGRPCFramework extends BaseFramework<
public getFrameworkName() {
return 'gRPC';
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('appLogger');
}
}
6 changes: 2 additions & 4 deletions packages/kafka/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class MidwayKafkaFramework extends BaseFramework<
IKafkaConsumerInitOptions,
IKafkaConsumer
>;
protected frameworkLoggerName = 'kafkaLogger';

configure() {
return this.configService.getConfiguration('kafka');
}
Expand Down Expand Up @@ -316,8 +318,4 @@ export class MidwayKafkaFramework extends BaseFramework<
await this.typedResourceManager.destroy();
}
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('kafkaLogger');
}
}
5 changes: 0 additions & 5 deletions packages/mock/src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
ObjectIdentifier,
isTypeScriptEnvironment,
DecoratorManager,
ILogger,
} from '@midwayjs/core';
import { isAbsolute, join, resolve } from 'path';
import { clearAllLoggers, loggers } from '@midwayjs/logger';
Expand Down Expand Up @@ -602,10 +601,6 @@ class LightFramework extends BaseFramework<any, any, any, any, any> {
getFrameworkName(): string {
return 'lightFramework';
}

getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('appLogger');
}
}

class BootstrapAppStarter implements IBootstrapAppStarter {
Expand Down
16 changes: 4 additions & 12 deletions packages/mqtt/src/framework.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
Framework,
BaseFramework,
Logger,
DecoratorManager,
MetadataManager,
ILogger,
} from '@midwayjs/core';
import {
IMidwayMQTTApplication,
Expand All @@ -24,9 +22,7 @@ export class MidwayMQTTFramework extends BaseFramework<
> {
public app: IMidwayMQTTApplication;
protected subscriberMap: Map<string, MqttClient> = new Map();

@Logger('mqttLogger')
mqttLogger: ILogger;
protected frameworkLoggerName = 'mqttLogger';

configure() {
return this.configService.getConfiguration('mqtt');
Expand All @@ -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'
);
}
Expand Down Expand Up @@ -69,7 +65,7 @@ export class MidwayMQTTFramework extends BaseFramework<
protected async beforeStop(): Promise<void> {
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`);
}
}

Expand Down Expand Up @@ -107,7 +103,7 @@ export class MidwayMQTTFramework extends BaseFramework<
resolve(client);
});
client.on('error', err => {
this.mqttLogger.error(err);
this.logger.error(err);
});
});

Expand Down Expand Up @@ -143,8 +139,4 @@ export class MidwayMQTTFramework extends BaseFramework<
public getFrameworkName() {
return 'mqtt';
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('mqttLogger');
}
}
5 changes: 0 additions & 5 deletions packages/rabbitmq/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DecoratorManager,
MetadataManager,
listPropertyDataFromClass,
ILogger,
} from '@midwayjs/core';
import {
IMidwayRabbitMQApplication,
Expand Down Expand Up @@ -121,8 +120,4 @@ export class MidwayRabbitMQFramework extends BaseFramework<
public getFrameworkName() {
return 'rabbitmq';
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('appLogger');
}
}
5 changes: 0 additions & 5 deletions packages/socketio/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
WSEventInfo,
WSEventTypeEnum,
Framework,
ILogger,
} from '@midwayjs/core';

@Framework()
Expand Down Expand Up @@ -345,8 +344,4 @@ export class MidwaySocketIOFramework extends BaseFramework<
> {
return this.connectionMiddlewareManager;
}

public getFrameworkLogger(): ILogger {
return this.loggerService.getLogger('appLogger');
}
}
Loading

0 comments on commit 821ea82

Please sign in to comment.