tsioc
Version:
tsioc is AOP, Ioc container, via typescript decorator
102 lines (100 loc) • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("../aop/index");
var index_2 = require("../utils/index");
var DefaultLogConfigure_1 = require("./DefaultLogConfigure");
var LoggerAspect = /** @class */ (function () {
function LoggerAspect(container, config) {
this.container = container;
this._config = config;
}
Object.defineProperty(LoggerAspect.prototype, "config", {
get: function () {
if (!this._config) {
if (!this.container.has(index_2.symbols.LogConfigure)) {
this.container.register(DefaultLogConfigure_1.DefaultLogConfigure);
}
this._config = this.container.resolve(index_2.symbols.LogConfigure);
}
return this._config;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LoggerAspect.prototype, "logger", {
get: function () {
if (!this._logger) {
this._logger = this.logManger.getLogger();
}
return this._logger;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LoggerAspect.prototype, "logManger", {
get: function () {
if (!this._logManger) {
this._logManger = this.container.resolve(this.config.adapter || 'console');
if (this.config.config) {
this._logManger.configure(this.config.config);
}
}
return this._logManger;
},
enumerable: true,
configurable: true
});
LoggerAspect.prototype.processLog = function (joinPoint, annotation) {
var _this = this;
if (annotation && annotation.length) {
annotation.forEach(function (logmeta) {
var canlog = false;
if (logmeta.express && logmeta.express(joinPoint)) {
canlog = true;
}
else if (!logmeta.express) {
canlog = true;
}
if (canlog) {
_this.writeLog(logmeta.logname ? _this.logManger.getLogger(logmeta.logname) : _this.logger, joinPoint, logmeta.message);
}
});
}
else {
this.writeLog(this.logger, joinPoint);
}
};
LoggerAspect.prototype.writeLog = function (logger, joinPoint, message) {
var isCustom = index_2.isFunction(this.config.customFormat);
if (message) {
logger.info(message);
}
if (isCustom) {
this.config.customFormat(joinPoint, logger);
}
else if (this.config.format) {
var formatStr = index_2.isFunction(this.config.format) ? this.config.format(joinPoint, logger) : '';
if (!formatStr) {
return;
}
var formatArgs = index_2.isFunction(this.config.formatArgs) ? this.config.formatArgs(joinPoint, logger) : [];
switch (joinPoint.state) {
case index_1.JoinpointState.Before:
case index_1.JoinpointState.After:
case index_1.JoinpointState.AfterReturning:
logger.debug.apply(logger, [formatStr].concat(formatArgs));
break;
case index_1.JoinpointState.Pointcut:
logger.info.apply(logger, [formatStr].concat(formatArgs));
break;
case index_1.JoinpointState.AfterThrowing:
logger.error.apply(logger, [formatStr].concat(formatArgs));
break;
}
}
};
LoggerAspect.classAnnations = { "name": "LoggerAspect", "params": { "constructor": ["container", "config"], "logging": ["joinPoint", "annotation"], "processLog": ["joinPoint", "annotation"], "writeLog": ["logger", "joinPoint", "message"] } };
return LoggerAspect;
}());
exports.LoggerAspect = LoggerAspect;
//# sourceMappingURL=sourcemaps/LoggerAspect.js.map