UNPKG

@ts-ioc/logs

Version:

tsioc is AOP, Ioc container, via typescript decorator

125 lines (123 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var core_1 = require("@ts-ioc/core"); var aop_1 = require("@ts-ioc/aop"); /** * Log formater interface token. * it is a token id, you can register yourself formater for log. */ exports.LogFormaterToken = new core_1.InjectToken('DI_LogFormater'); var LogFormater = /** @class */ (function () { function LogFormater() { } LogFormater.prototype.format = function (joinPoint, message) { var pointMsg; switch (joinPoint.state) { case aop_1.JoinpointState.Before: case aop_1.JoinpointState.Pointcut: pointMsg = joinPoint.state + " invoke method \"" + joinPoint.fullName + "\"\n with args: " + this.stringifyArgs(joinPoint.params, joinPoint.args) + ".\n"; break; case aop_1.JoinpointState.After: pointMsg = joinPoint.state + " invoke method \"" + joinPoint.fullName + "\".\n"; break; case aop_1.JoinpointState.AfterReturning: pointMsg = "Invoke method \"" + joinPoint.fullName + "\"\n returning value: " + this.stringify(joinPoint.returningValue) + ".\n"; break; case aop_1.JoinpointState.AfterThrowing: pointMsg = "Invoke method \"" + joinPoint.fullName + "\"\n throw error: " + this.stringify(joinPoint.throwing) + ".\n"; break; default: pointMsg = ''; break; } return this.joinMessage([pointMsg, message]); }; LogFormater.prototype.stringifyArgs = function (params, args) { var _this = this; var argsStr = params.map(function (p, idx) { var arg = args.length >= idx ? args[idx] : null; return " <param name: \"" + (p.name || '') + "\"> " + _this.stringify(arg); }).join(',\n'); if (argsStr) { return this.joinMessage([' [\n', argsStr, '\n]'], ''); } else { return '[]'; } }; LogFormater.prototype.joinMessage = function (messgs, separator) { if (separator === void 0) { separator = '; '; } return messgs.filter(function (a) { return a; }).map(function (a) { return core_1.isString(a) ? a : a.toString(); }).join(separator); }; LogFormater.prototype.stringifyArray = function (args) { var _this = this; if (!args.length) { return '[]'; } return '[\n' + args.map(function (arg) { return _this.stringify(arg); }).join(',\n') + '\n].'; }; LogFormater.prototype.stringify = function (target) { var type = typeof target; var str = ''; switch (type) { case 'string': str = "\"" + target + "\""; break; case 'boolean': str = target.toString(); break; case 'number': str = target.toString(); break; case 'symbol': str = target.toString(); break; case 'object': if (core_1.isNull(target)) { str = 'null'; } else if (core_1.isArray(target)) { str = this.stringifyArray(target); } else if (core_1.isDate(target)) { str = "[Date: " + target.toString() + "]"; } else if (target instanceof core_1.Container) { str = "[" + core_1.lang.getClassName(target) + "]"; } else { str = "[" + core_1.lang.getClassName(target) + ": " + this.toJsonString(target) + "]"; } break; default: if (core_1.isClass(target)) { str = "[class: " + core_1.lang.getClassName(target) + "]"; } else if (core_1.isFunction(target)) { str = "[function: " + core_1.lang.getClassName(target) + "]"; } break; } return str; }; LogFormater.prototype.toJsonString = function (target) { try { return JSON.stringify(target); } catch (err) { return 'object'; } }; LogFormater.getClassAnnations = function () { return { "name": "LogFormater", "params": { "constructor": [], "format": ["joinPoint", "message"], "stringifyArgs": ["params", "args"], "joinMessage": ["messgs", "separator"], "stringifyArray": ["args"], "stringify": ["target"], "toJsonString": ["target"] } }; }; LogFormater = tslib_1.__decorate([ aop_1.NonePointcut(), core_1.Singleton(exports.LogFormaterToken), tslib_1.__metadata("design:paramtypes", []) ], LogFormater); return LogFormater; }()); exports.LogFormater = LogFormater; //# sourceMappingURL=sourcemaps/LogFormater.js.map