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

251
Views
javascript custom console log function keeping stack trace

I have a helper function that prepends some message when calling console.log:

function log(...arg: any[]): void {
  const LOG_PREFIX = `[file.ts]`;
  // 1. Convert args to a normal array
  const args: any[] = Array.prototype.slice.call(arguments);
  // 2. Prepend log prefix log string
  args.unshift(LOG_PREFIX);
  // 3. Pass along arguments to console.log
  console.log.call(console, ...args);
}

The problem is that when it logs, the console shows a clickable link to this log:8 or the line that calls console.log.call(console, ...args)

Is there a way I can adjust this method so that it keeps the intended stack trace of where i call this method instead?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

There you go, Function.prototype.bind() is to the rescue:

const log = console.log.bind(window.console, "[file.ts]");

log("test", "blah");
console.log("test", "blah");

about 4 years ago · Santiago Gelvez 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!