I made this function based on another answer here from StackOverflow, as I say in the first comment on the function. Hope it's useful to someone else! Call logLnDebug() , not the weird one.
function logLnDebug(message) { console.debug("D:" + ___8drrd3148796d_Xaf() + message); } let __correct_stack_line_idx_GL = -1; function ___8drrd3148796d_Xaf() { // Adapted from: https://stackoverflow.com/a/26410435/8228163. const err = new Error(); const stack_lines = err.stack.split('\n'); if (__correct_stack_line_idx_GL === -1) { for (let i = 0; i < stack_lines.length; i++) { let element_split = stack_lines[i].trim().split(' '); for (const part of element_split) { let part_trimmed = part.trim(); if (part_trimmed === "___8drrd3148796d_Xaf") { // Chrome __correct_stack_line_idx_GL = i; break; } else if (part_trimmed.includes("@")) { // Firefox let at_split = part_trimmed.split('@'); if (at_split.length >= 2 && at_split[0] === "___8drrd3148796d_Xaf") { __correct_stack_line_idx_GL = i; break; } } } } } let file_name = "ERROR"; let line_number = -1; // stack_lines[0] is "Error", stack_lines[1] is this function, stack_lines[2] is the caller if (stack_lines.length >= __correct_stack_line_idx_GL + 2 + 1) { // Example stack line: " at callerFunction (file.js:10:5)" // Another example: "EventHandlerNonNull*checkWebSocket@http://localhost:63344/Test/js/Test.js:160:13" // And another example: "at http://localhost:63344/Test/Test.html?_ijt=b3b2k4v2u542v6gnonnpivb0e4&_ij_reload=RELOAD_ON_SAVE:137:13" let stack_line = stack_lines[__correct_stack_line_idx_GL + 2].trim(); let dot_idx = stack_line.indexOf('.'); let last_slash_idx = stack_line.lastIndexOf('/'); let last_colon_idx = stack_line.lastIndexOf(':'); let second_last_colon_idx = stack_line.lastIndexOf(':', last_colon_idx - 1); if (last_colon_idx > dot_idx && second_last_colon_idx > dot_idx) { line_number = stack_line.substring(second_last_colon_idx + 1, last_colon_idx); file_name = stack_line.substring(last_slash_idx + 1, second_last_colon_idx); } else if (last_colon_idx > dot_idx && second_last_colon_idx < last_slash_idx) { line_number = stack_line.substring(last_slash_idx + 1); file_name = stack_line.substring(last_slash_idx + 1, last_colon_idx); } else { // Keep line_number as -1 (non-existent) file_name = stack_line.substring(last_slash_idx + 1); } if (file_name.indexOf('?') !== -1) { file_name = file_name.substring(0, file_name.indexOf('?')); } } return file_name + ":" + line_number + "|> "; }
This should print for example D:ThisIsATest.js:166|> Hello! . Works for me on Firefox and Chrome.
Then to make it prettier in the output, you can print only the uppercase letters of the file name without the extension and pad to a maximum number of characters on that and the line number. Would become D: TIAT:166 |> Hello! in that case. JetBrains IDEs can find the right files with the uppercase letters and the line numbers, that's why I have it like this.
Note: I just realized the question is for Node. This pure JS for the browser. Sorry if it's not useful for this case. I'll leave it here anyway in case anyone finds it useful.
chrome://inspect?