Skip to content

Instantly share code, notes, and snippets.

@anurbol
Created July 28, 2018 11:27

Revisions

  1. anurbol created this gist Jul 28, 2018.
    25 changes: 25 additions & 0 deletions console.log-extended.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    ["log", "warn", "error"].forEach((methodName) => {
    const originalMethod = console[methodName]
    console[methodName] = (...args) => {
    let initiator = "unknown place"
    try {
    throw new Error()
    } catch (e) {
    if (typeof e.stack === "string") {
    let isFirst = true
    for (const line of e.stack.split("\n")) {
    const matches = line.match(/^\s+at\s+(.*)/)
    if (matches) {
    if (!isFirst) { // first line - current function
    // second line - caller (what we are looking for)
    initiator = matches[1]
    break
    }
    isFirst = false
    }
    }
    }
    }
    originalMethod.apply(console, [...args, "\n", ` at ${initiator}`])
    }
    })