-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BaseContextClass add logger (#816)
- move BaseContextClass to egg - add BaseContextLogger
- Loading branch information
1 parent
9871e45
commit 0757655
Showing
14 changed files
with
254 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
const BaseContextLogger = require('./base_context_logger'); | ||
|
||
const LOGGER = Symbol('BaseContextClass#logger'); | ||
|
||
/** | ||
* BaseContextClass is a base class that can be extended, | ||
* it's instantiated in context level, | ||
* {@link Helper}, {@link Service} is extending it. | ||
*/ | ||
class BaseContextClass { | ||
|
||
/** | ||
* @constructor | ||
* @param {Context} ctx - context instance | ||
* @since 1.0.0 | ||
*/ | ||
constructor(ctx) { | ||
/** | ||
* @member {Context} BaseContextClass#ctx | ||
* @since 1.0.0 | ||
*/ | ||
this.ctx = ctx; | ||
/** | ||
* @member {Application} BaseContextClass#app | ||
* @since 1.0.0 | ||
*/ | ||
this.app = ctx.app; | ||
/** | ||
* @member {Config} BaseContextClass#config | ||
* @since 1.0.0 | ||
*/ | ||
this.config = ctx.app.config; | ||
/** | ||
* @member {Service} BaseContextClass#service | ||
* @since 1.0.0 | ||
*/ | ||
this.service = ctx.service; | ||
} | ||
|
||
get logger() { | ||
if (!this[LOGGER]) this[LOGGER] = new BaseContextLogger(this.ctx, this.pathName); | ||
return this[LOGGER]; | ||
} | ||
} | ||
|
||
module.exports = BaseContextClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const CALL = Symbol('BaseContextLogger#call'); | ||
|
||
class BaseContextLogger { | ||
|
||
/** | ||
* @constructor | ||
* @param {Context} ctx - context instance | ||
* @param {String} pathName - class path name | ||
* @since 1.0.0 | ||
*/ | ||
constructor(ctx, pathName) { | ||
/** | ||
* @member {Context} BaseContextLogger#ctx | ||
* @since 1.2.0 | ||
*/ | ||
this.ctx = ctx; | ||
this.pathName = pathName; | ||
} | ||
|
||
[CALL](method, args) { | ||
// add `[${pathName}]` in log | ||
if (this.pathName && typeof args[0] === 'string') { | ||
args[0] = `[${this.pathName}] ${args[0]}`; | ||
} | ||
this.ctx.logger[method](...args); | ||
} | ||
|
||
/** | ||
* @member {Function} BaseContextLogger#debug | ||
* @since 1.2.0 | ||
*/ | ||
debug(...args) { | ||
this[CALL]('debug', args); | ||
} | ||
|
||
/** | ||
* @member {Function} BaseContextLogger#info | ||
* @since 1.2.0 | ||
*/ | ||
info(...args) { | ||
this[CALL]('info', args); | ||
} | ||
|
||
/** | ||
* @member {Function} BaseContextLogger#warn | ||
* @since 1.2.0 | ||
*/ | ||
warn(...args) { | ||
this[CALL]('warn', args); | ||
} | ||
|
||
/** | ||
* @member {Function} BaseContextLogger#error | ||
* @since 1.2.0 | ||
*/ | ||
error(...args) { | ||
this[CALL]('error', args); | ||
} | ||
} | ||
|
||
module.exports = BaseContextLogger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/fixtures/apps/base-context-class/app/controller/home.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
return class HomeController extends app.Controller { | ||
* show() { | ||
yield this.service.home.show(); | ||
this.ctx.body = 'hello'; | ||
this.logger.debug('debug'); | ||
this.logger.info('appname: %s', this.config.name); | ||
this.logger.warn('warn'); | ||
this.logger.error(new Error('some error')); | ||
} | ||
|
||
getPathName() { | ||
this.ctx.body = this.pathName; | ||
} | ||
|
||
getConfig() { | ||
this.ctx.body = this.config.name; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.get('/', 'home.show'); | ||
app.get('/pathName', 'home.getPathName'); | ||
app.get('/config', 'home.getConfig'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
return class HomeController extends app.Service { | ||
* show() { | ||
this.ctx.body = 'hello'; | ||
this.logger.debug('debug'); | ||
this.logger.info('appname: %s', this.config.name); | ||
this.logger.warn('warn'); | ||
this.logger.error(new Error('some error')); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/apps/base-context-class/config/config.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
exports.keys = 'test keys'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "base-context-class" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters