winston-transport-logtail
Version:
logtail.com transport for winston v3 logger
69 lines • 2.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStackContext = void 0;
const path_1 = require("path");
const stack_trace_1 = __importDefault(require("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
*/
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(),
},
},
};
}
exports.getStackContext = getStackContext;
function getCallingFrame(logtail) {
for (let fn of [
logtail.info,
logtail.warn,
logtail.error,
logtail.log,
logtail,
]) {
const stack = stack_trace_1.default.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 = (0, path_1.dirname)(mainFileName());
return (0, path_1.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