3

I have a project that has JavaScript in 2 places. One is using node.js on the server-side while the other is obviously the browser JavaScript. I am required to support any browser and I'd like to have a single logging framework for all of them. I picked log4js which works great for logging strings but for objects it has a bit of difficulty.

In node when I do Logger.debug(someObject); I get a beautiful printout of the object. When I try to do this with my browser JavaScript I get [object Object] which is useless.

Anyone know how to do this?

(node is using the terminal as the output console and the browser JavaScript is using the Browser's (Chrome) Console)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
trixtur
  • 708
  • 6
  • 14
  • You can expand objects in the chrome console if you are not logging them as strings? – Bergi Feb 12 '13 at 23:19
  • Yes, but log4js seems to just do a 'toString' on the objects which just prints out [object Object] – trixtur Feb 12 '13 at 23:36
  • [log4javascript](http://log4javascript.org/) supports this already but has no Node.js support yet. The next version will run in Node.js as well as the browser. – Tim Down Feb 13 '13 at 09:26
  • Tim - I don't believe this is the case. First of all, I did npm install log4js and it installed perfectly, with the same version and the code is from the same authors. Second of all I read through the javascript and found no way to do this without modifying the code as suggested in the accepted answer. – trixtur Feb 13 '13 at 19:00
  • @trixtur: [log4js](http://log4js.berlios.de/) and [log4javascript](http://log4javascript.org/) are not the same thing. – Tim Down Feb 15 '13 at 16:01
  • oh sorry, thats me not reading very closely. – trixtur Feb 19 '13 at 16:13

1 Answers1

4

log4js is still pretty young in terms of development. I would modify the source code around line 1795 in "doAppend"

if(typeof(loggingEvent.message) === 'object')
    return window.console.log(loggingEvent.level.levelStr + " - %o", loggingEvent.message);
ben.payne
  • 71
  • 3