diff --git a/app/extend/context.js b/app/extend/context.js index 9b1fbdd0c7..175aacb3ff 100644 --- a/app/extend/context.js +++ b/app/extend/context.js @@ -1,13 +1,9 @@ 'use strict'; const delegate = require('delegates'); -const ContextLogger = require('egg-logger').EggContextLogger; -const Cookies = require('egg-cookies'); const co = require('co'); -const ContextHttpClient = require('../../lib/core/context_httpclient'); const { assign } = require('utility'); - const HELPER = Symbol('Context#helper'); const LOCALS = Symbol('Context#locals'); const LOCALS_LIST = Symbol('Context#localsList'); @@ -18,7 +14,7 @@ const CONTEXT_HTTPCLIENT = Symbol('Context#httpclient'); const proto = module.exports = { get cookies() { if (!this[COOKIES]) { - this[COOKIES] = new Cookies(this, this.app.keys); + this[COOKIES] = new this.app.ContextCookies(this, this.app.keys); } return this[COOKIES]; }, @@ -30,7 +26,7 @@ const proto = module.exports = { */ get httpclient() { if (!this[CONTEXT_HTTPCLIENT]) { - this[CONTEXT_HTTPCLIENT] = new ContextHttpClient(this); + this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this); } return this[CONTEXT_HTTPCLIENT]; }, @@ -96,7 +92,7 @@ const proto = module.exports = { if (!appLogger) return null; // write to cache - cache[name] = new ContextLogger(this, appLogger); + cache[name] = new this.app.ContextLogger(this, appLogger); return cache[name]; }, diff --git a/lib/egg.js b/lib/egg.js index 049701b8cf..3fb78f1630 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -5,6 +5,9 @@ const fs = require('fs'); const EggCore = require('egg-core').EggCore; const cluster = require('cluster-client'); const extend = require('extend2'); +const ContextLogger = require('egg-logger').EggContextLogger; +const ContextCookies = require('egg-cookies'); +const ContextHttpClient = require('./core/context_httpclient'); const Messenger = require('./core/messenger'); const createHttpClient = require('./core/httpclient'); const createLoggers = require('./core/logger'); @@ -34,6 +37,11 @@ class EggApplication extends EggCore { constructor(options) { super(options); + // export context base classes, let framework can impl sub class and over context extend easily. + this.ContextCookies = ContextCookies; + this.ContextLogger = ContextLogger; + this.ContextHttpClient = ContextHttpClient; + this.loader.loadConfig(); /** diff --git a/test/app/extend/context.test.js b/test/app/extend/context.test.js index 3cade4d924..50ce381d02 100644 --- a/test/app/extend/context.test.js +++ b/test/app/extend/context.test.js @@ -130,6 +130,21 @@ describe('test/app/extend/context.test.js', () => { }); }); + describe('app or framework can override ctx.getLogger', () => { + let app; + before(() => { + app = utils.app('apps/custom-context-getlogger'); + return app.ready(); + }); + after(() => app.close()); + + it('should log with custom logger', () => { + return request(app.callback()) + .get('/') + .expect('work, logger: exists'); + }); + }); + describe('properties', () => { let app; before(() => { diff --git a/test/fixtures/apps/custom-context-getlogger/app/controller/home.js b/test/fixtures/apps/custom-context-getlogger/app/controller/home.js new file mode 100644 index 0000000000..6e9c3f6ce3 --- /dev/null +++ b/test/fixtures/apps/custom-context-getlogger/app/controller/home.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = function* () { + const logger = this.getLogger('foo'); + logger.info('hello'); + this.body = 'work, logger: ' + (logger ? 'exists' : 'not exists'); +}; diff --git a/test/fixtures/apps/custom-context-getlogger/app/extend/context.js b/test/fixtures/apps/custom-context-getlogger/app/extend/context.js new file mode 100644 index 0000000000..264d7e950f --- /dev/null +++ b/test/fixtures/apps/custom-context-getlogger/app/extend/context.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = { + getLogger(name) { + console.log('get custom %s logger', name); + return new this.app.ContextLogger(this, console); + }, +}; diff --git a/test/fixtures/apps/custom-context-getlogger/app/router.js b/test/fixtures/apps/custom-context-getlogger/app/router.js new file mode 100644 index 0000000000..e28728a8a1 --- /dev/null +++ b/test/fixtures/apps/custom-context-getlogger/app/router.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = app => { + app.get('/', 'home'); +}; diff --git a/test/fixtures/apps/custom-context-getlogger/config/config.default.js b/test/fixtures/apps/custom-context-getlogger/config/config.default.js new file mode 100644 index 0000000000..5365ab0bf2 --- /dev/null +++ b/test/fixtures/apps/custom-context-getlogger/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.keys = 'foo'; diff --git a/test/fixtures/apps/custom-context-getlogger/package.json b/test/fixtures/apps/custom-context-getlogger/package.json new file mode 100644 index 0000000000..617eda9f55 --- /dev/null +++ b/test/fixtures/apps/custom-context-getlogger/package.json @@ -0,0 +1,3 @@ +{ + "name": "custom-context-getlogger" +}