apitally
Version:
Simple API monitoring & analytics for REST APIs built with Express, Fastify, NestJS, AdonisJS, Hono, H3, Elysia, Hapi, and Koa.
68 lines • 1.86 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { formatMessage, removeKeys } from "./utils.js";
const MAX_BUFFER_SIZE = 1e3;
let isPatched = false;
let globalLogsContext;
async function patchWinston(logsContext) {
var _a, _b;
globalLogsContext = logsContext;
if (isPatched) {
return;
}
try {
const loggerModule = await import(
// @ts-expect-error - file is not typed
/* webpackIgnore: true */
"winston/lib/winston/logger.js"
);
if ((_b = (_a = loggerModule.default) == null ? void 0 : _a.prototype) == null ? void 0 : _b.write) {
const originalWrite = loggerModule.default.prototype.write;
loggerModule.default.prototype.write = function(info) {
captureLog(info);
return originalWrite.call(this, info);
};
}
} catch {
}
isPatched = true;
}
__name(patchWinston, "patchWinston");
function captureLog(info) {
const logs = globalLogsContext == null ? void 0 : globalLogsContext.getStore();
if (!logs || !info || logs.length >= MAX_BUFFER_SIZE) {
return;
}
try {
const rest = removeKeys(info, [
"timestamp",
"level",
"message",
"splat"
]);
const formattedMessage = formatMessage(info.message, rest);
if (formattedMessage) {
logs.push({
timestamp: parseTimestamp(info.timestamp),
level: info.level || "info",
message: formattedMessage.trim()
});
}
} catch (e) {
}
}
__name(captureLog, "captureLog");
function parseTimestamp(timestamp) {
if (timestamp) {
const ts = new Date(timestamp).getTime();
if (!isNaN(ts)) {
return ts / 1e3;
}
}
return Date.now() / 1e3;
}
__name(parseTimestamp, "parseTimestamp");
export {
patchWinston
};
//# sourceMappingURL=winston.js.map