Estoy usando Winston logger con la especificación EntryLog para iniciar sesión y quiero obtener estos datos del new Error().stack :
{ file: "index.js", function: "getData", line: 20, column: 50 }Lo descubro:
/** * Parses and returns info about the call stack. */ function getSouceLocation(error) { // get call stack, and analyze it and // remove unnecessary stacktrace [node_modules, <anonymous>] // get all file, method, and line numbers const stacklist = error.stack .replace(/^.*[\\/]node_modules[\\/].*$|^.((?!at).)*$|^.*<anonymous>.*$|^.*internal\/timers.js.*$/gm, "") .replace(/\n+/g, "\n").split("\n") .filter((item, index, array) => { if (!!item) { return index === array.indexOf(item); } }); // stack trace format: // http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi // do not remove the regex expresses to outside of this method (due to a BUG in node.js) var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi var stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi const sources = [] stacklist.forEach((item) => { var sp = stackReg.exec(item) || stackReg2.exec(item) if (sp && sp.length === 5) { sources.push( { function: sp[1], file: path.relative(PROJECT_ROOT, sp[2]), line: sp[3], column: sp[4], } ) } }); const stack = stacklist.join('\n'); return { sources, stack }; }Resultado:
"sourceLocation": { "sources": [ { "column": "36", "file": "src\\app\\controllers\\informasi\\panduan\\panduan-search.js", "function": "module.exports", "line": "11" }, { "column": "7", "file": "src\\utils\\callback.js", "function": "", "line": "18" } ], "stack": "SequelizeDatabaseError: column a.nourutt does not exist\n at module.exports (D:\\GOLANG\\gocode\\src\\github.com\\oss-rba\\back-portal\\src\\app\\controllers\\informasi\\panduan\\panduan-search.js:11:36)\n at D:\\GOLANG\\gocode\\src\\github.com\\oss-rba\\back-portal\\src\\utils\\callback.js:18:7" },Fuente: winston logger con nombre de archivo: número de línea