LPS-133670 we added support for log context information. This mean any app can populate their own log context (i.e. key-value pairs) and it will be included in the output of the logs. For example, this is the case of the AuditLogContext (see LPS-133646). While the feature is working, as we've tried to process the output logs with tools such as StackDrive, we realized that the LogContext JSON are not well formed. For instance, in this trace:
AuditLogContext is missing quotes.
Since log4j allows to customize the output format through Layouts in the portal-log4j-ext.xml file, we considered that it'd be a better approach to simple make the logcontext values available in the logs, and define their formatting through the portal-log4j-ext.xml
Log4j2 introduced the TheadContext (see https://logging.apache.org/log4j/2.x/manual/thread-context.html) as a way to inject custom information into the log context. The information can latter be displayed in the logs by configuring the log layouts.
- Given an application producing custom log info via LogContext services in Liferay, when the log4j is configured to include the log4j thread context properties in the log, then the logs include the properties in the defined format.
How to test it:
- Copy the portal-impl/src/META-INF/portal-log4j.xml into bundles/tomcat/webapps/ROOT/WEB-INF/classes/META-INF
- Rename the file to portal-log4j-ext.xml
- Modify the first log appender to include the ThreadContext properties:
- Notice the %X in the Layout pattern. As documented in https://logging.apache.org/log4j/2.x/manual/thread-context.html, this option includes the full threadContext properties in the log.
- Start the portal
- Go to Control Panel > System Settings > Audit > Audit Log Context, check "Enabled" and Save
- Go to Control Panel > Server Administration > Log Levels and add a new category for "com.liferay.commerce" with level DEBUG
- Force a log trace of com.liferay.commerce. For example, by executing:
- Assert that the log contains the Audit Log Context information, in the position defined in the layout pattern of the portal-log4j-ext.xml:
Produce a valid JSON output:
To produce a valid JSON-formatted log output including the thread context properties, change the configuration in the porta-log4j-ext.xml file like this and restart:
Then, by repeating the steps to test, you will obtain the following output in the logs: