Skip to content

Commit

Permalink
fix: disable coreLogger info console output in local env (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jan 28, 2021
1 parent 7941f87 commit adaaaea
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/web-express/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ export class MidwayExpressFramework extends BaseFramework<
[];

this.logger.info(
`Load Router "${webRouter.requestMethod} ${webRouter.path}"`
`Load Router "${webRouter.requestMethod.toUpperCase()} ${
webRouter.path
}"`
);
// apply controller from request context
newRouter[webRouter.requestMethod].call(
Expand Down
12 changes: 7 additions & 5 deletions packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export abstract class MidwayKoaBaseFramework<
];

this.logger.info(
`Load Router "${webRouter.requestMethod} ${webRouter.path}"`
`Load Router "${webRouter.requestMethod.toUpperCase()} ${
webRouter.path
}"`
);

// apply controller from request context
Expand Down Expand Up @@ -276,6 +278,10 @@ export abstract class MidwayKoaBaseFramework<
}
}
}

public getDefaultContextLoggerClass() {
return MidwayKoaContextLogger;
}
}

export class MidwayKoaFramework extends MidwayKoaBaseFramework<
Expand Down Expand Up @@ -362,8 +368,4 @@ export class MidwayKoaFramework extends MidwayKoaBaseFramework<
public getServer() {
return this.server;
}

public getDefaultContextLoggerClass() {
return MidwayKoaContextLogger;
}
}
5 changes: 0 additions & 5 deletions packages/web/config/config.local.js

This file was deleted.

9 changes: 7 additions & 2 deletions packages/web/src/framework/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@midwayjs/core';
import { ControllerOption, CONFIG_KEY, PLUGIN_KEY } from '@midwayjs/decorator';
import { IMidwayWebConfigurationOptions } from '../interface';
import { MidwayKoaBaseFramework, MidwayKoaContextLogger } from '@midwayjs/koa';
import { MidwayKoaBaseFramework } from '@midwayjs/koa';
import { EggRouter } from '@eggjs/router';
import { Application, Context, Router, EggLogger } from 'egg';
import { loggers } from '@midwayjs/logger';
Expand All @@ -32,6 +32,10 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this.configurationOptions = options;
// set default context logger
this.BaseContextLoggerClass =
options.ContextLoggerClass || this.getDefaultContextLoggerClass();

if (options.typescript === false) {
this.isTsMode = false;
}
Expand Down Expand Up @@ -64,9 +68,10 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
);

if (this.app.config.midwayFeature['replaceEggLogger']) {
// if use midway logger will be use midway custom context logger
Object.defineProperty(this.app, 'ContextLogger', {
get() {
return MidwayKoaContextLogger;
return self.BaseContextLoggerClass;
},
});
}
Expand Down
12 changes: 10 additions & 2 deletions packages/web/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ class MidwayLoggers extends Map<string, ILogger> {
if (this.app.getProcessType() === MidwayProcessTypeEnum.AGENT) {
this.createLogger(
'coreLogger',
{ file: options.logger.agentLogName },
{
file: options.logger.agentLogName,
level: options.logger?.coreLogger?.level,
consoleLevel: options.logger?.coreLogger?.consoleLevel,
},
options.logger,
'agent:coreLogger'
);
Expand All @@ -145,7 +149,11 @@ class MidwayLoggers extends Map<string, ILogger> {
} else {
this.createLogger(
'coreLogger',
{ file: options.logger.coreLogName },
{
file: options.logger.coreLogName,
level: options.logger?.coreLogger?.level,
consoleLevel: options.logger?.coreLogger?.consoleLevel,
},
options.logger,
'coreLogger'
);
Expand Down
15 changes: 14 additions & 1 deletion packages/web/test/feature.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { closeApp, creatApp, createHttpRequest } from './utils';
import { closeApp, creatApp, createHttpRequest, matchContentTimes, sleep } from './utils';
import { IMidwayWebApplication } from '../src/interface';
import { join } from 'path';

describe('/test/feature.test.ts', () => {
describe('test new decorator', () => {
Expand Down Expand Up @@ -67,4 +68,16 @@ describe('/test/feature.test.ts', () => {
await closeApp(app);
});

it('should test set custom logger in egg by midway logger', async () => {
const app = await creatApp('feature/base-app-set-ctx-logger');
const result = await createHttpRequest(app)
.get('/')
.query({ name: 'harry' });
expect(result.status).toEqual(200);
expect(result.text).toEqual('hello world,harry');
await sleep();
expect(matchContentTimes(join(app.getAppDir(), 'logs', 'ali-demo', 'midway-web.log'), 'custom label')).toEqual(1);
await closeApp(app);
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};

export const midwayFeature = {
replaceEggLogger: true,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.hello = {
b: 4,
c: 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Configuration, App } from '@midwayjs/decorator';
import { MidwayCustomContextLogger } from './logger';

@Configuration({
importConfigs: [
'./config'
]
})
export class ContainerConfiguration {
@App()
app: any;

async onReady() {
this.app.setContextLoggerClass(MidwayCustomContextLogger);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
Controller,
Get,
Provide,
Inject,
Query,
} from '@midwayjs/decorator';

@Provide()
@Controller('/')
export class APIController {
@Inject()
ctx: any;

@Get('/')
async home(@Query('name') name: string) {
this.ctx.logger.info('custom label');
return 'hello world,' + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MidwayContextLogger } from '@midwayjs/core';
import { Context } from 'egg';

export class MidwayCustomContextLogger extends MidwayContextLogger<Context> {
formatContextLabel() {
const ctx = this.ctx;
return `${Date.now() - ctx.startTime}ms ${ctx.method}`;
}
}

0 comments on commit adaaaea

Please sign in to comment.