@Override public String format(LoggingEvent loggingEvent) { String loggerName = loggingEvent.getLoggerName(); String level = loggingEvent.getLevel().toString(); String message = loggingEvent.getMessage().toString().trim(); if(!message.startsWith("{") && !message.startsWith("[")){ message = "\"" + message + "\""; } String threadName = loggingEvent.getThreadName(); Date timeStamp = new Date(loggingEvent.getTimeStamp()); String context = Context.toJSON(); ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation(); String exception = ""; if(throwableInformation != null){ exception = ",\"exception\":{\"message\":\""; Throwable throwable = throwableInformation.getThrowable(); String exceptionMessage = throwable.getMessage() != null? throwable.getMessage(): ""; //need to be careful here, sanitizing, since the message may already contain a chunk of JSON, so escaping or cleaning double quotes is not prudent:) exception += sanitize(exceptionMessage, false, '\n', '\t', '\r') + "\",\"stacktrace\":\"" + escapeControlChars(Util.getStackTraceString(throwable)) + "\"}"; } String contextJson = context != null ? ",\"context\":" + context : ""; String timestampString = this.simpleDateFormat == null ? timeStamp.toString() : simpleDateFormat.format(timeStamp); return "{\"level\":\"" + level + "\",\"timestamp\":\"" + timestampString + "\",\"thread\":\"" + threadName + "\",\"logger\":\"" + loggerName + "\",\"message\":" + message + contextJson + exception + "}" + System.getProperty("line.separator"); }
@Override public String format(LoggingEvent loggingEvent) { String loggerName = loggingEvent.getLoggerName(); String level = loggingEvent.getLevel().toString(); String message = loggingEvent.getMessage().toString().trim(); if(!message.startsWith("{") && !message.startsWith("[")){ message = "\"" + message + "\""; } String threadName = loggingEvent.getThreadName(); Date timeStamp = new Date(loggingEvent.getTimeStamp()); String context = Context.toJSON(); ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation(); String exception = ""; if(throwableInformation != null){ exception = ",\"exception\":{\"message\":\""; Throwable throwable = throwableInformation.getThrowable(); String exceptionMessage = throwable.getMessage() != null? throwable.getMessage(): ""; //need to be careful here, sanitizing, since the message may already contain a chunk of JSON, so escaping or cleaning double quotes is not prudent:) exception += sanitize(exceptionMessage, false, '\n', '\t', '\r') + "\",\"stacktrace\":\"" + escapeControlChars(Util.getStackTraceString(throwable)) + "\"}"; } String contextJson = context != null ? ",\"context\":" + context : ""; String timestampString = this.simpleDateFormat == null ? timeStamp.toString() : simpleDateFormat.format(timeStamp); return "{\"level\":\"" + level + "\",\"timestamp\":\"" + timestampString + "\",\"thread\":\"" + threadName + "\",\"logger\":\"" + loggerName + "\",\"message\":" + message + contextJson + exception + "}" + System.getProperty("line.separator"); }