Fix: Ensure the Java JUL messages are correctly formatted
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Thu, 4 Feb 2016 20:02:57 +0000 (15:02 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 4 Feb 2016 20:13:28 +0000 (15:13 -0500)
It is possible for log records to contain messages that need some
formatting, for example if the string contains localized elements
or if the log(Level, String, Object[]) method is used.

In these cases, we need to make sure to format the string and not
pass the "raw" string to the tracepoint.

This only applies to the JUL API. log4j 1.2.x did not handle such
formatting, although log4j 2.x does.

This is a backport of commit 4721f9c to the stable-2.7 branch.

Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-java-agent/java/org/lttng/ust/agent/jul/LTTngLogHandler.java

index b844d2f675b50760cacb635ada85a8b1f409e2a1..15c7a8ea2829140b53d0ea36a8188ec6820c8aeb 100644 (file)
@@ -19,11 +19,23 @@ package org.lttng.ust.agent.jul;
 
 import java.lang.String;
 
+import java.util.logging.Formatter;
 import java.util.logging.Handler;
 import java.util.logging.LogRecord;
 
 class LTTngLogHandler extends Handler {
 
+       /**
+        * Dummy Formatter object, so we can use its
+        * {@link Formatter#formatMessage(LogRecord)} method.
+        */
+       private static final Formatter FORMATTER = new Formatter() {
+               @Override
+               public String format(LogRecord record) {
+                       throw new UnsupportedOperationException();
+               }
+       };
+
        private final Boolean isRoot;
 
        public LTTngLogHandler(Boolean isRoot) {
@@ -54,18 +66,20 @@ class LTTngLogHandler extends Handler {
 
        @Override
        public void publish(LogRecord record) {
+               String formattedMessage = FORMATTER.formatMessage(record);
+
                /*
                 * Specific tracepoint designed for JUL events. The source class of the
                 * caller is used for the event name, the raw message is taken, the
                 * loglevel of the record and the thread ID.
                 */
                if (this.isRoot) {
-                       tracepointS(record.getMessage(),
+                       tracepointS(formattedMessage,
                                    record.getLoggerName(), record.getSourceClassName(),
                                    record.getSourceMethodName(), record.getMillis(),
                                    record.getLevel().intValue(), record.getThreadID());
                } else {
-                       tracepointU(record.getMessage(),
+                       tracepointU(formattedMessage,
                                    record.getLoggerName(), record.getSourceClassName(),
                                    record.getSourceMethodName(), record.getMillis(),
                                    record.getLevel().intValue(), record.getThreadID());
This page took 0.025806 seconds and 4 git commands to generate.