benbria / bunyan-debug-stream

A stream for bunyan that writes log entries as human readable output.
MIT License
30 stars 9 forks source link

Logging object with circular reference #13

Closed agoops closed 5 years ago

agoops commented 5 years ago

Hi, I noticed that when trying to log an object with a circular reference, bunyan-debug-stream throws an error: TypeError: Converting circular structure to JSON.

It seems that's the case because it calls JSON.stringify here.

Is there a way to override that with something likeutil.inspect so that circular references are handled when converted to strings?

An example case showing the circular reference:

var bunyanDebugStream = require('bunyan-debug-stream');
var bunyan = require('bunyan');

var log = bunyan.createLogger({
    name: "myLog",
    streams: [{
        level:  'info',
        type:   'raw',
        stream: bunyanDebugStream({
            forceColor: true
        })
    }],
});

let b = {a: "temp"};
const a = {b: b};
b.a = a;

log.info(a);

throws

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at BunyanDebugStream._entryToString (/temp/node_modules/bunyan-debug-stream/lib/BunyanDebugStream.js:206:30)
    at BunyanDebugStream._write (/temp/node_modules/bunyan-debug-stream/lib/BunyanDebugStream.js:253:28)
    at doWrite (_stream_writable.js:397:12)
    at writeOrBuffer (_stream_writable.js:383:5)
    at BunyanDebugStream.Writable.write (_stream_writable.js:290:11)
    at Logger._emit (/temp/node_modules/bunyan/lib/bunyan.js:923:22)
    at Logger.info (/temp/node_modules/bunyan/lib/bunyan.js:1045:24)
    at Object.<anonymous> (/bunyandebug-test.js:20:5)
    at Module._compile (module.js:652:30)

Thanks!

jwalton commented 5 years ago

Good catch! I'd happily take a PR.

agoops commented 5 years ago

Realized a quick fix that one can set the type of stream to stream instead of raw. That way, bunyan will serialize if before passing it to bunyan-debug-stream.

So an updated config would look like

var bunyanDebugStream = require('bunyan-debug-stream');
var bunyan = require('bunyan');

var log = bunyan.createLogger({
    name: "myLog",
    streams: [{
        level:  'info',
        type:   'stream',
        stream: bunyanDebugStream({
            forceColor: true
        })
    }],
});