Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
How to get filename, function, line, and column from Error for EntryLog.SourceLocation

I am using winston logger with EntryLog spec for logging, and I wanna get these data from new Error().stack:

{
  file: "index.js",
  function: "getData",
  line: 20,
  column: 50
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I figure it out:

/**
 * 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 };
}

Result:

"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"
  },

Source: winston logger with filename:linenumber

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!