UNPKG

winston-transport-logtail

Version:
62 lines 1.84 kB
import { dirname, relative } from "path"; import stackTrace from "stack-trace"; /** * Determines the file name and the line number from which the log * was initiated (if we're able to tell). * * @returns Context The caller's filename and the line number */ export function getStackContext(logtail) { const stackFrame = getCallingFrame(logtail); if (stackFrame === null) return {}; return { context: { runtime: { file: relativeToMainModule(stackFrame.getFileName()), type: stackFrame.getTypeName(), method: stackFrame.getMethodName(), function: stackFrame.getFunctionName(), line: stackFrame.getLineNumber(), column: stackFrame.getColumnNumber(), }, system: { pid: process.pid, main_file: mainFileName(), }, }, }; } function getCallingFrame(logtail) { for (let fn of [ logtail.info, logtail.warn, logtail.error, logtail.log, logtail, ]) { const stack = stackTrace.get(fn); if (stack.length > 0) { return stack[0]; } } return null; } function relativeToMainModule(fileName) { if (typeof fileName !== "string") { return null; } else if (fileName.startsWith("file:/")) { const url = new URL(fileName); return url.pathname; } else { const rootPath = dirname(mainFileName()); return relative(rootPath, fileName); } } function mainFileName() { var _a, _b; return (_b = (_a = require === null || require === void 0 ? void 0 : require.main) === null || _a === void 0 ? void 0 : _a.filename) !== null && _b !== void 0 ? _b : ""; } //# sourceMappingURL=context.js.map