Fix: Ensure the Java JUL messages are correctly formatted
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Thu, 4 Feb 2016 05:32:01 +0000 (00:32 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 4 Feb 2016 14:19:26 +0000 (09:19 -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.

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

index d8e0cb826eb40f3e37fd75d2b1ec68907b16ba66..39e08a831d51614ec3e5bcd4bc8e9d338ab7456e 100644 (file)
@@ -20,6 +20,7 @@ package org.lttng.ust.agent.jul;
 
 import java.io.IOException;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.logging.Formatter;
 import java.util.logging.Handler;
 import java.util.logging.LogRecord;
 
@@ -43,6 +44,17 @@ public class LttngLogHandler extends Handler implements ILttngHandler {
 
        private static final String SHARED_OBJECT_NAME = "lttng-ust-jul-jni";
 
+       /**
+        * 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 ILttngAgent<LttngLogHandler> agent;
 
        /** Number of events logged (really sent through JNI) by this handler */
@@ -103,13 +115,15 @@ public class LttngLogHandler extends Handler implements ILttngHandler {
                        return;
                }
 
+               String formattedMessage = FORMATTER.formatMessage(record);
+
                eventCount.incrementAndGet();
                /*
                 * 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.
                 */
-               tracepoint(record.getMessage(),
+               tracepoint(formattedMessage,
                                record.getLoggerName(),
                                record.getSourceClassName(),
                                record.getSourceMethodName(),
This page took 0.026319 seconds and 5 git commands to generate.