Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda