From a9d7a0dab8db24c970fe6528deb62afcf24c11b0 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Mon, 8 Feb 2021 11:59:21 +0800 Subject: [PATCH] fix: ctx logger overwrite in egg extend (#846) --- packages/web/agent.js | 7 +++++++ packages/web/app.js | 4 ++++ packages/web/app/extend/agent.js | 13 +++++++++++++ packages/web/src/framework/web.ts | 5 +++++ packages/web/test/feature.test.ts | 5 +++-- .../feature/base-app-set-ctx-logger/src/agent.ts | 10 ++++++++++ .../feature/base-app-set-ctx-logger/src/app.ts | 10 ++++++++++ .../base-app-set-ctx-logger/src/configuration.ts | 2 ++ .../feature/base-app-set-ctx-logger/src/logger.ts | 2 +- packages/web/test/utils.ts | 2 +- 10 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 packages/web/app/extend/agent.js create mode 100644 packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/agent.ts create mode 100644 packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/app.ts diff --git a/packages/web/agent.js b/packages/web/agent.js index 69b4d7d525f2..e4f60f3f7dab 100644 --- a/packages/web/agent.js +++ b/packages/web/agent.js @@ -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() {} diff --git a/packages/web/app.js b/packages/web/app.js index af86f327f3a0..eeb1e2d753cd 100644 --- a/packages/web/app.js +++ b/packages/web/app.js @@ -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() { diff --git a/packages/web/app/extend/agent.js b/packages/web/app/extend/agent.js new file mode 100644 index 000000000000..18efe6ee1c51 --- /dev/null +++ b/packages/web/app/extend/agent.js @@ -0,0 +1,13 @@ +module.exports = { + get baseDir() { + return this.loader.baseDir; + }, + + get appDir() { + return this.loader.appDir; + }, + + get webFramework() { + return this.loader.framework; + }, +}; diff --git a/packages/web/src/framework/web.ts b/packages/web/src/framework/web.ts index 2a5455f30cd2..7596d02e8643 100644 --- a/packages/web/src/framework/web.ts +++ b/packages/web/src/framework/web.ts @@ -215,4 +215,9 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework< } return null; } + + protected setContextLoggerClass(BaseContextLogger: any) { + this.BaseContextLoggerClass = BaseContextLogger; + this.app.ContextLogger = BaseContextLogger; + } } diff --git a/packages/web/test/feature.test.ts b/packages/web/test/feature.test.ts index 8ace0e8bfc6c..479484c4f359 100644 --- a/packages/web/test/feature.test.ts +++ b/packages/web/test/feature.test.ts @@ -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', () => { @@ -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); }); diff --git a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/agent.ts b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/agent.ts new file mode 100644 index 000000000000..cfde47d063a7 --- /dev/null +++ b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/agent.ts @@ -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'); +} diff --git a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/app.ts b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/app.ts new file mode 100644 index 000000000000..cb0db9fd94da --- /dev/null +++ b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/app.ts @@ -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'); +} diff --git a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/configuration.ts b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/configuration.ts index 3bd3a172281f..981f8a78791e 100644 --- a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/configuration.ts +++ b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/configuration.ts @@ -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'); } } diff --git a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/logger.ts b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/logger.ts index 76ded50da6a7..f6f04fb24ceb 100644 --- a/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/logger.ts +++ b/packages/web/test/fixtures/feature/base-app-set-ctx-logger/src/logger.ts @@ -4,6 +4,6 @@ import { Context } from 'egg'; export class MidwayCustomContextLogger extends MidwayContextLogger { formatContextLabel() { const ctx = this.ctx; - return `${Date.now() - ctx.startTime}ms ${ctx.method}`; + return `${Date.now() - ctx.startTime}ms ${ctx.method} abcde`; } } diff --git a/packages/web/test/utils.ts b/packages/web/test/utils.ts index fdddd9167c59..37a8449084ed 100644 --- a/packages/web/test/utils.ts +++ b/packages/web/test/utils.ts @@ -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) || [];