var _ = require('underscore');
var layouts = require('log4js').layouts;
var sprintf = require("sprintf-js").sprintf;
var stringify = require('json-stringify-safe');
var NestedError = require('nested-error-stacks');
var isPlainObject = require('is-plain-object');

var Log4jsJsonLayout = {
  process: function(logEvent) {
    var logEntry = {
      time: logEvent.startTime.toISOString(),
      level: logEvent.level && logEvent.level.levelStr || null
    };

    try {
      var data = logEvent.data;
      if (Array.isArray(data)) {
        var params = this.extractParams(data);
        logEntry = _.extend(logEntry, params);
      }
      return stringify(logEntry);
    } catch (e) {
      return JSON.stringify(new NestedError('Error converting log message to JSON', e));
    }
  },

  extractParams: function(logData) {
    var messages = [];
    var extra = {};
    logData.forEach(function(element) {
      if (isPlainObject(element)) {
        extra = _.extend(extra, element);
      } else {
        messages.push(element);
      }
    });
    var parsedMessages = this.parseMessages(messages);
    extra['message'] = parsedMessages.shift();
    if (parsedMessages.length > 0) {
      extra['additionalMessages'] = parsedMessages;
    }
    return extra;
  },

  parseMessages: function(messages) {
    if (typeof messages[0] === 'string' && messages[0].indexOf('%') >= 0) {
      return [sprintf.apply(this, messages)];
    }
    return messages;
  }
};

layouts.addLayout('json', function() {
  return Log4jsJsonLayout.process.bind(Log4jsJsonLayout);
});

@@ -476,76 +476,17 @@
 var 
-data = logEvent.data;%0A      if (Array.isArray(data)) %7B%0A        var p
+logEntryP
 aram
@@ -512,17 +512,24 @@
 ams(
+logEvent.
 data);%0A
-  
     
@@ -564,24 +564,24 @@
 ry, 
-params);%0A      %7D
+logEntryParams);
 %0A   
@@ -798,21 +798,22 @@
     var 
-extra
+params
  = %7B%7D;%0A 
@@ -895,21 +895,22 @@
         
-extra
+params
  = _.ext
@@ -913,21 +913,22 @@
 .extend(
-extra
+params
 , elemen
@@ -1053,21 +1053,22 @@
 s);%0A    
-extra
+params
 %5B'messag
@@ -1140,21 +1140,22 @@
 %7B%0A      
-extra
+params
 %5B'additi
@@ -1204,21 +1204,22 @@
  return 
-extra
+params
 ;%0A  %7D,%0A%0A

Streamline even more