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

103
Views
Save Console.log output to variable

This question is similar in nature to Converting an object to a string

console.log works awfully good. It can log very deep objects, self-referencing objects, nulls, undefined, multiparams...

How can I store the output of the function as a string? I want to have my own logging system, but I want to keep the same format as console.log.

The most voted answer for the referenced question does not work with self referencing objects. Nor does .toString():

> a = {}
{}
> a.b = a
<ref *1> { b: [Circular *1] }
> a
<ref *1> { b: [Circular *1] }
> a.b
<ref *1> { b: [Circular *1] }
> JSON.stringify(a)
Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    --- property 'b' closes the circle
    at JSON.stringify (<anonymous>)
> console.log(a)
<ref *1> { b: [Circular *1] }
undefined
> a.toString()
'[object Object]'

Basically I would want some function stringify with

> stringify(a) === '<ref *1> { b: [Circular *1] }'

The question is also similar to How can I print a circular structure in a JSON-like format? but not exactly. The question there is to log one self referencing object. I'm asking for something more generic, which is something like the console output. In particular, console handles gracefully multiparams

about 4 years ago · Juan Pablo Isaza
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!