Logging complex/circular objects
Opened this issue · 1 comments
Hey,
i am using log.trace({ msg: 'objects', data: { obj1: obj1, obj2: obj2 } });
to log objects. This, however, doesn't work, if an object has circular references.
ERROR TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at Function.MessageFormatUtils.renderDefaultLog4jMessage (MessageUtils.js?4da4:151)
at ConsoleLoggerImpl.AbstractLogger.createDefaultLogMessage (AbstractLogger.js?3e76:198)
at ConsoleLoggerImpl.doLog (ConsoleLoggerImpl.js?f3bc:30)
at ConsoleLoggerImpl.AbstractLogger.processMessages (AbstractLogger.js?3e76:259)
at ConsoleLoggerImpl.AbstractLogger._log (AbstractLogger.js?3e76:224)
at ConsoleLoggerImpl.AbstractLogger.debug (AbstractLogger.js?3e76:151)
at HomeComponent.printUserInfo (home.component.ts?945e:37)
at HomeComponent.ngOnInit (home.component.ts?945e:27)
at checkAndUpdateDirectiveInline (core.js?09c9:10105)
The problem can be 'fixed' by using a smarter object inspection library than plain stringify.
Is that something you would consider adding?
If you overhaul the logging of objects, i would also really love to see the following functionality:
a) pretty print the objects instead of inline
b) supply objects as positional parameters to the message
To clarify b):
Right now, i can do
log.trace(`obj1: ${obj1}, obj2: ${obj2}`);
but then i have to stringify/format the objects myself.
I would prefer a C or log4j style of logging, e.g.:
// C style
log.trace("obj1: %s, obj2: %s", obj1, obj2);
// log4j
log.trace("obj1: {}, obj2: {}", obj1, obj2);
// Or even positionally aware
log.trace("obj1: {0}, obj2: {1}, obj1 again: {0}", obj1, obj2);
Thank you for this very flexible logging framework.
Hello @serialoverflow
Thank you for logging this. They are nice suggestions.
I like the idea of actually dealing with circular references, so that sounds like a reasonable enhancement to me.
Pretty print may be an option to add (as in to enable if wanted), I am not sure what you want to print "pretty" exactly here though? A json object or something else? I guess I need some simple example here.
The C or positionally aware log lines have my preference from your suggestions, although I don't know yet if that can be easily included in the current release, because I would likely have to break API for that, and would prefer to avoid that.
I may have to finalize this version to a 1.0.0 later, and then start "breaking" stuff in a 2.x version.