UNPKG

@dyihoon90/glogging

Version:

HTTP request logging middleware & transaction function decorator for express, using winston

114 lines 5.49 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GLoggerAuditLogger = void 0; var core_1 = require("@js-joda/core"); var _1 = require("."); var GLoggerAuditLogger = /** @class */ (function () { function GLoggerAuditLogger(glogger) { this.glogger = glogger; } GLoggerAuditLogger.prototype.logHttpSuccess = function (message, _a, _b) { var req = _a.req, res = _a.res; var trxName = _b.trxName, trxModule = _b.trxModule, filename = _b.filename; var logData = { trxCategory: _1.TransactionCategory.HTTP, trxId: req.uuid || '', trxName: trxName, trxModule: trxModule, filename: filename, userToken: req.user, timeTakenInMillis: req.reqStartTimeInEpochMillis ? core_1.Duration.between(core_1.Instant.ofEpochMilli(req.reqStartTimeInEpochMillis), core_1.ZonedDateTime.now()).toMillis() : undefined, trxStatus: _1.TransactionStatus.SUCCESS, additionalInfo: { url: req.url, method: req.method, srcIp: req.ip, statusCode: res.statusCode } }; this.glogger.info(message, __assign({}, logData)); return this; }; GLoggerAuditLogger.prototype.logHttpFailure = function (error, _a, _b) { var req = _a.req, res = _a.res; var trxName = _b.trxName, trxModule = _b.trxModule, filename = _b.filename; var logData = { trxCategory: _1.TransactionCategory.HTTP, trxId: req.uuid || '', trxName: trxName, trxModule: trxModule, filename: filename, userToken: req.user, timeTakenInMillis: req.reqStartTimeInEpochMillis ? core_1.Duration.between(core_1.Instant.ofEpochMilli(req.reqStartTimeInEpochMillis), core_1.ZonedDateTime.now()).toMillis() : undefined, trxStatus: _1.TransactionStatus.FAILURE, additionalInfo: { url: req.url, method: req.method, srcIp: req.ip, statusCode: res.statusCode } }; this.glogger.warn(error.name, error, __assign({}, logData)); return this; }; GLoggerAuditLogger.prototype.logTransactionSuccess = function (message, _a, _b, trxStartTimeInEpochMillis, result) { var req = _a.req; var trxCategory = _b.trxCategory, trxName = _b.trxName, trxModule = _b.trxModule, filename = _b.filename; var logData = { trxCategory: trxCategory, trxId: (req === null || req === void 0 ? void 0 : req.uuid) || '', trxModule: trxModule, trxName: trxName, filename: filename, userToken: req === null || req === void 0 ? void 0 : req.user, timeTakenInMillis: core_1.Duration.between(core_1.Instant.ofEpochMilli(trxStartTimeInEpochMillis), core_1.ZonedDateTime.now()).toMillis(), trxStatus: _1.TransactionStatus.SUCCESS, additionalInfo: { url: req === null || req === void 0 ? void 0 : req.url, method: req === null || req === void 0 ? void 0 : req.method } }; if (logData.additionalInfo && result) { logData.additionalInfo.result = result; } this.glogger.info(message, __assign({}, logData)); return this; }; GLoggerAuditLogger.prototype.logTransactionFailure = function (_a, _b, trxStartTimeInEpochMillis, error) { var req = _a.req; var trxCategory = _b.trxCategory, trxName = _b.trxName, trxModule = _b.trxModule, filename = _b.filename; var logData = { trxCategory: trxCategory, trxId: (req === null || req === void 0 ? void 0 : req.uuid) || '', trxName: trxName, trxModule: trxModule, filename: filename, userToken: req === null || req === void 0 ? void 0 : req.user, timeTakenInMillis: core_1.Duration.between(core_1.Instant.ofEpochMilli(trxStartTimeInEpochMillis), core_1.ZonedDateTime.now()).toMillis(), trxStatus: _1.TransactionStatus.FAILURE, additionalInfo: { url: req === null || req === void 0 ? void 0 : req.url, method: req === null || req === void 0 ? void 0 : req.method } }; // Promise.reject() by convention should reject with Error. // but in scenarios where it rejects with other things, we still try our best to log the object if (error instanceof Error) { this.glogger.warn(error.message, error, __assign({}, logData)); } else if (typeof error === 'string') { this.glogger.warn(error, undefined, __assign({}, logData)); } else { this.glogger.warn('error', undefined, __assign(__assign({}, logData), { additionalInfo: __assign(__assign({}, logData.additionalInfo), { error: error }) })); } return this; }; return GLoggerAuditLogger; }()); exports.GLoggerAuditLogger = GLoggerAuditLogger; //# sourceMappingURL=GLogger.auditLogger.js.map