Log object in log4javascript

I want to register objects using log4javascript. For example, consider the following code:

function LogObject() { var blah = { one: 42, two: "486" }; logger.Info(blah); 

Assuming the log is an instance of log4javascript logger that is configured correctly:

 var logger = log4javascript.getLogger("InternalLogger"); var ajaxAppender = new log4javascript.AjaxAppender(url), jsonLayout = new log4javascript.JsonLayout(false, false); ajaxAppender.setLayout(jsonLayout); ajaxAppender.addHeader("Content-Type", "application/json"); logger.addAppender(ajaxAppender); 

I expect the result to be as follows: request loadload contains an array of messages, the first of which is my object serialized in JSON. I see an array of messages, the first of which has the string "Object of the object" (for example, the toString () method was called). How can i achieve this?

+4
source share
3 answers

JsonLayout formats the log event (which includes the log level, timestamp, and log name in addition to the log messages) as JSON, rather than the log message, which is pretty much considered a string. The reason for this is to avoid depending on the JSON library for older browsers; generating JSON for the simple, well-known data that JsonLayout deals JsonLayout , without a problem with the JSON library, but definitely one thing is needed to process arbitrary objects.

The workaround I suggest is to simply format the message before passing it to the registration call:

 logger.info( JSON.stringify(blah) ); 
+4
source

We followed @Tim Down's suggestion

 logger.info( JSON.stringify(blah) ); 

But we had performance issues, since JSON.stringify happens before logger.info is logger.info , so it will always happen even if the logging level is set to ignore this log.

To get around this, I wrote a new lazy layout so that streaming only happens if the log is actually displayed. To be more flexible, it can also pass a function, in which case it outputs the result of the specified function.

Application:

 logger.trace("Received ", widget, " which has ", () => countFrimbles(widget), ' frimbles'); 

Implementation:

 function LazyFormatLayout() { } LazyFormatLayout.prototype = new log4javascript.Layout(); LazyFormatLayout.prototype.format = function (loggingEvent) { var time = loggingEvent.timeStamp.toTimeString().split(/\s/)[0]; var head = time + ' ' + loggingEvent.logger.name + ' [' + loggingEvent.level.name + '] - '; var body = loggingEvent.messages.map(function (arg) { try { switch (typeof (arg)) { case 'function': return arg(); case 'object': return JSON.stringify(arg); } } catch (e) { return '<<error while logging: ' + e.stack + '>>'; } return arg; }).join(''); if (!loggingEvent.exception) return head + body; return head + body + ' ==> Exception: ' + loggingEvent.exception.stack; } LazyFormatLayout.prototype.ignoresThrowable = function () { return false; }; LazyFormatLayout.prototype.toString = function () { return "LazyFormatLayout"; }; 
+3
source

The question is somewhat outdated, but a simple Google search has raised this question, and there seems to be a built-in way to register objects:

var log = log4javascript.getDefaultLogger(); log.info("log following object",{ data:5, text:"bla" });

Exit

12:49:43 INFO - write the following object {data: 5, text: bla}

0
source

Source: https://habr.com/ru/post/1496745/


All Articles