Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ctx logger overwrite in egg extend #846

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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