Skip to content

Commit

Permalink
LDEV-4752 - add support for json message in jsonLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Nov 16, 2023
1 parent b3adcf7 commit 8f1fec6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import lucee.loader.engine.CFMLEngineFactory;
import lucee.loader.util.Util;
import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
import lucee.runtime.converter.ConverterException;
import lucee.runtime.converter.JSONConverter;
import lucee.runtime.converter.JSONDateFormat;
import lucee.runtime.engine.ThreadLocalPageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.interpreter.JSONExpressionInterpreter;
import lucee.runtime.op.Caster;
import lucee.runtime.security.Credential;
import lucee.runtime.type.Array;
Expand Down Expand Up @@ -166,12 +168,26 @@ public String toSerializable(final LogEvent event) {
}
}
}
// extract message
String strMSG;
if (jsonSupported) {
root.setEL("message", ((MultiformatMessage) msg).getFormattedMessage(FORMATS));
strMSG = ((MultiformatMessage) msg).getFormattedMessage(FORMATS);
}
else {
root.setEL("message", msg.getFormattedMessage());
strMSG = msg.getFormattedMessage();
}
if (strMSG == null) strMSG = "";

// split application name
int index = strMSG.indexOf("->");
String application;
if (index > -1) {
application = strMSG.substring(0, index);
strMSG = strMSG.substring(index + 2);
}
else application = "";
root.setEL("application", application);
root.setEL("message", toJson(strMSG, strMSG));
}

// Thrown
Expand Down Expand Up @@ -219,8 +235,19 @@ public String toSerializable(final LogEvent event) {
// thread
{
Thread thread = Thread.currentThread();
root.setEL("threadId", thread.getId());
root.setEL("threadId", thread.getPriority());
Struct th = util.createStruct("linked");
th.setEL("id", thread.getId());
th.setEL("priority", thread.getPriority());
root.setEL("thread", th);
}

// page context
PageContext pc = ThreadLocalPageContext.get();
if (pc != null) {
root.setEL("pageContextId", pc.getId());
if (pc instanceof PageContextImpl) {
root.setEL("requestId", ((PageContextImpl) pc).getRequestId());
}
}

if (this.locationInfo) {
Expand Down Expand Up @@ -313,6 +340,23 @@ public String toSerializable(final LogEvent event) {

}

private static Object toJson(String strJson, Object defaultValue) {
if (StringUtil.isEmpty(strJson)) {
return defaultValue;
}

if (!(strJson.startsWith("{") && strJson.endsWith("}") || strJson.startsWith("[") && strJson.endsWith("]"))) {
return defaultValue;
}

try {
return new JSONExpressionInterpreter().interpret(ThreadLocalPageContext.get(), strJson);
}
catch (PageException e) {
return defaultValue;
}
}

private Object createStacktrace(Creation util, StackTraceElement[] stackTraces, boolean stacktraceAsString) throws PageException {

// Stacktrace
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.0.1.26-SNAPSHOT"/>
<property name="version" value="6.0.1.27-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.0.1.26-SNAPSHOT</version>
<version>6.0.1.27-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 8f1fec6

Please sign in to comment.