Is there a way to override the file/line information reported by console.log?

Viewed 100

I have a transpiler that generates JavaScript code from a custom language. The print statements from the original language get translated into console.log. However, when the output of console log shows up in the developer console, it obviously displays the JavaScript file name and line number but that isn't very useful to my users.

Is there a way to override the file name and line number for console.log so that I can have my code generator pass in the original file and line information?

Turning this: enter image description here

...into this: enter image description here

1 Answers

Trying to figure this out myself has led me here, so I'll share what I found after an afternoon of research: apparently not. The best you can do is to add the original file&line information to be included within your log message, resulting in logs like these:

2/6/2021, 22:27:06 [info ] [background-main.js:31] loaded user settings from localStorage:
{
 "settings": {
  "downloadAttachments": true,
  "useLostAndFound": true,
  "debug": true,
  "downloadInterval": "3000"
 }
}
2/6/2021, 22:27:06 [info ] [download.js:95] started download interval (intervalID "2") with "3000" ms interval
2/6/2021, 22:27:06 [info ] [background-main.js:51] wrote settings to localStorage:
{
 "downloadAttachments": true,
 "useLostAndFound": true,
 "debug": true,
 "downloadInterval": "3000"
}
Related