Skip to content

Commit

Permalink
fix: ctx logger overwrite in egg extend (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Feb 8, 2021
1 parent 0051d07 commit a9d7a0d
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/web/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class AgentBootHook {
this.app = app;
}

configDidLoad() {
if (this.app.config.midwayFeature['replaceEggLogger']) {
// if use midway logger will be use midway custom context logger
this.app.ContextLogger = this.app.webFramework.BaseContextLoggerClass;
}
}

async didLoad() {}

async willReady() {}
Expand Down
4 changes: 4 additions & 0 deletions packages/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class AppBootHook {
this.app.loader.config.coreMiddleware = [];
this.appMiddleware = this.app.loader.config.appMiddleware;
this.app.loader.config.appMiddleware = [];
if (this.app.config.midwayFeature['replaceEggLogger']) {
// if use midway logger will be use midway custom context logger
this.app.ContextLogger = this.app.webFramework.BaseContextLoggerClass;
}
}

async didLoad() {
Expand Down
13 changes: 13 additions & 0 deletions packages/web/app/extend/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
get baseDir() {
return this.loader.baseDir;
},

get appDir() {
return this.loader.appDir;
},

get webFramework() {
return this.loader.framework;
},
};
5 changes: 5 additions & 0 deletions packages/web/src/framework/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,9 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
}
return null;
}

protected setContextLoggerClass(BaseContextLogger: any) {
this.BaseContextLoggerClass = BaseContextLogger;
this.app.ContextLogger = BaseContextLogger;
}
}
5 changes: 3 additions & 2 deletions packages/web/test/feature.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { closeApp, creatApp, createHttpRequest, matchContentTimes, sleep } from './utils';
import { IMidwayWebApplication } from '../src/interface';
import { IMidwayWebApplication } from '../src';
import { join } from 'path';

describe('/test/feature.test.ts', () => {
Expand Down Expand Up @@ -76,7 +76,8 @@ describe('/test/feature.test.ts', () => {
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);
expect(matchContentTimes(join(app.getAppDir(), 'logs', 'ali-demo', 'midway-web.log'), 'GET /] aaaaa')).toEqual(3);
expect(matchContentTimes(join(app.getAppDir(), 'logs', 'ali-demo', 'midway-web.log'), 'abcde] custom label')).toEqual(1);
await closeApp(app);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Application } from 'egg';
import * as assert from 'assert';
import { join } from 'path';

module.exports = (app: Application) => {
assert(app.baseDir === __dirname);
assert((app as any).appDir === join(__dirname, '..'));
assert((app as any).applicationContext);
app.createAnonymousContext().logger.warn('aaaaa');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as assert from 'assert';
import { join } from 'path';
import { Application } from 'egg';

module.exports = (app: Application) => {
assert(app.baseDir === __dirname);
assert((app as any).appDir === join(__dirname, '..'));
assert((app as any).applicationContext);
app.createAnonymousContext().logger.warn('aaaaa');
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export class ContainerConfiguration {
app: any;

async onReady() {
this.app.createAnonymousContext().logger.warn('aaaaa');
this.app.setContextLoggerClass(MidwayCustomContextLogger);
this.app.createAnonymousContext().logger.warn('ccccc');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Context } from 'egg';
export class MidwayCustomContextLogger extends MidwayContextLogger<Context> {
formatContextLabel() {
const ctx = this.ctx;
return `${Date.now() - ctx.startTime}ms ${ctx.method}`;
return `${Date.now() - ctx.startTime}ms ${ctx.method} abcde`;
}
}
2 changes: 1 addition & 1 deletion packages/web/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const matchContentTimes = (p: string, matchString: string | RegExp) => {
}

if (typeof matchString === 'string') {
matchString = new RegExp(matchString);
matchString = new RegExp(matchString, 'g');
}

const result = content.match(matchString) || [];
Expand Down

0 comments on commit a9d7a0d

Please sign in to comment.