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 775dd51 commit e57dac2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 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 All @@ -63,10 +65,10 @@ public class JsonLayout extends AbstractStringLayout { // TODO <Serializable>
private boolean doComma = true;
private final Charset charset;
private boolean compact;
private boolean complete;
private boolean hasEnvLoaded;
private Object token = new Object();
private StructImpl envs;
private boolean complete;
private String[] envNames;

// private static final DateFormat dateFormat = new DateFormat(Locale.US);
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,10 @@ 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);
}

if (this.locationInfo) {
Expand Down Expand Up @@ -259,6 +277,11 @@ public String toSerializable(final LogEvent event) {
}
else user = remoteUser.getUsername();
if (!Util.isEmpty(user, true)) root.setEL("authUser", user);

root.setEL("pageContextId", pc.getId());
if (pc instanceof PageContextImpl) {
root.setEL("requestId", ((PageContextImpl) pc).getRequestId());
}
}

// env var
Expand Down Expand Up @@ -313,6 +336,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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class IsJSON {
public static boolean call(PageContext pc, Object obj) {
String str = Caster.toString(obj, null);
if (StringUtil.isEmpty(str, true)) return false;

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

try {
new JSONExpressionInterpreter().interpret(pc, str);
return true;
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="5.4.4.33-SNAPSHOT"/>
<property name="version" value="5.4.4.34-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>5.4.4.33-SNAPSHOT</version>
<version>5.4.4.34-SNAPSHOT</version>
<packaging>jar</packaging>

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

0 comments on commit e57dac2

Please sign in to comment.