Add LOG4J2 domain to the Log4j 2.x agent
[lttng-ust.git] / src / lib / lttng-ust-java-agent / java / lttng-ust-agent-log4j2 / org / lttng / ust / agent / log4j2 / LttngLogAppender.java
index 9c093fa8ed0ee7dec54293277584457716d5ccfa..cebd18945e152cdb81ccc8461aa59e5a3b90eac3 100644 (file)
@@ -25,6 +25,8 @@ import org.apache.logging.log4j.core.config.plugins.Plugin;
 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.message.Message;
+import org.lttng.ust.agent.ILttngAgent.Domain;
 import org.lttng.ust.agent.ILttngHandler;
 import org.lttng.ust.agent.context.ContextInfoSerializer;
 
@@ -60,20 +62,24 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
         * Constructor
         *
         * @param name             The name of the Appender.
+        * @param domain           The LTTng-UST agent domain 'LOG4J' / 'LOG4J2'.
         * @param filter           The Filter or null.
-        * @param ignoreExceptions If {@code "true"} (default) exceptions encountered
-        *                         when appending events are logged; otherwise they are
+        * @param ignoreExceptions If {@code "true"} exceptions encountered when
+        *                         appending events are logged; otherwise they are
         *                         propagated to the caller.
         *
-        * @throws IOException       This handler requires the lttng-ust-log4j-jni.so
-        *                           native library, through which it will send the
-        *                           trace events. This exception is thrown if this
-        *                           library cannot be found.
-        * @throws SecurityException We will forward any SecurityExcepion that may be
-        *                           thrown when trying to load the JNI library.
+        * @throws IOException              This handler requires the
+        *                                  lttng-ust-log4j-jni.so native library,
+        *                                  through which it will send the trace events.
+        *                                  This exception is thrown if this library
+        *                                  cannot be found.
+        * @throws IllegalArgumentException If the provided domain is unsupported.
+        * @throws SecurityException        We will forward any SecurityExcepion that
+        *                                  may be thrown when trying to load the JNI
+        *                                  library.
         */
-       protected LttngLogAppender(String name, Filter filter, boolean ignoreExceptions)
-                       throws IOException, SecurityException {
+       protected LttngLogAppender(String name, LttngLog4j2Agent.Domain domain, Filter filter, boolean ignoreExceptions)
+                       throws IOException, IllegalArgumentException, SecurityException {
 
                super(name, filter, null, ignoreExceptions, Property.EMPTY_ARRAY);
 
@@ -85,7 +91,14 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
                }
 
                /* Register to the relevant agent. */
-               agent = LttngLog4j2Agent.getInstance();
+               if (domain == LttngLog4j2Agent.Domain.LOG4J) {
+                       agent = LttngLog4j2Agent.getLog4j1Instance();
+               } else if (domain == LttngLog4j2Agent.Domain.LOG4J2) {
+                       agent = LttngLog4j2Agent.getLog4j2Instance();
+               } else {
+                       throw new IllegalArgumentException("Unsupported domain '" + domain + "'");
+               }
+
                agent.registerHandler(this);
        }
 
@@ -93,35 +106,59 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
         * Create an LttngLogAppender.
         *
         * @param name             The name of the Appender, null returns null.
+        * @param domain           The LTTng-UST agent domain 'LOG4J' / 'LOG4J2'.
         * @param ignoreExceptions If {@code "true"} (default) exceptions encountered
         *                         when appending events are logged; otherwise they are
         *                         propagated to the caller.
         * @param filter           The Filter or null.
         *
-        * @return A new LttngLogAppender, null if the name was null.
-        *
-        * @throws IOException       This handler requires the lttng-ust-log4j-jni.so
-        *                           native library, through which it will send the
-        *                           trace events. This exception is thrown if this
-        *                           library cannot be found.
-        * @throws SecurityException We will forward any SecurityExcepion that may be
-        *                           thrown when trying to load the JNI library.
+        * @return A new LttngLogAppender, null if the name was null or the domain is
+        *         null or invalid.
         */
        @PluginFactory
        public static LttngLogAppender createAppender(@PluginAttribute("name") String name,
-                       @PluginAttribute("ignoreExceptions") Boolean ignoreExceptions, @PluginElement("Filters") Filter filter)
-                       throws IOException, SecurityException {
+                       @PluginAttribute("domain") String domain, @PluginAttribute("ignoreExceptions") Boolean ignoreExceptions,
+                       @PluginElement("Filters") Filter filter) {
 
                if (name == null) {
                        LOGGER.error("No name provided for LttngLogAppender");
                        return null;
                }
 
+               if (domain == null) {
+                       LOGGER.error("No domain provided for LttngLogAppender");
+                       return null;
+               }
+
                if (ignoreExceptions == null) {
                        ignoreExceptions = true;
                }
 
-               return new LttngLogAppender(name, filter, ignoreExceptions);
+               /* Parse the domain string */
+               LttngLog4j2Agent.Domain parsedDomain;
+               try {
+                       parsedDomain = LttngLog4j2Agent.Domain.valueOf(domain.toUpperCase());
+               } catch (IllegalArgumentException e) {
+                       LOGGER.error("Invalid domain '{}' for LttngLogAppender", domain);
+                       return null;
+               }
+
+               /* Create the appender and handle the possible failures. */
+               LttngLogAppender newAppender;
+               try {
+                       newAppender = new LttngLogAppender(name, parsedDomain, filter, ignoreExceptions);
+               } catch (IllegalArgumentException e) {
+                       LOGGER.error("Invalid domain '{}' for LttngLogAppender", parsedDomain);
+                       newAppender = null;
+               } catch (SecurityException e) {
+                       LOGGER.error("Security error trying to load '{}' JNI library for LttngLogAppender", SHARED_OBJECT_NAME);
+                       newAppender = null;
+               } catch (IOException e) {
+                       LOGGER.error("Failed to load '{}' JNI library for LttngLogAppender", SHARED_OBJECT_NAME);
+                       newAppender = null;
+               }
+
+               return newAppender;
        }
 
        @Override
@@ -164,10 +201,21 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
                 * Check if the current message should be logged, according to the UST session
                 * settings.
                 */
-               if (!agent.isEventEnabled(event.getLoggerName())) {
+               String loggername = event.getLoggerName();
+               if (loggername == null || !agent.isEventEnabled(loggername)) {
                        return;
                }
 
+               /*
+                * Default value if the Message is null.
+                */
+               String message = "";
+
+               Message eventMessage = event.getMessage();
+               if (eventMessage != null) {
+                       message = eventMessage.getFormattedMessage();
+               }
+
                /*
                 * Default values if the StackTraceElement is null.
                 */
@@ -191,8 +239,8 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
 
                eventCount.incrementAndGet();
 
-               LttngLog4j2Api.tracepointWithContext(event.getMessage().getFormattedMessage(), event.getLoggerName(), classname,
-                               methodname, filename, line, event.getTimeMillis(), event.getLevel().intLevel(), event.getThreadName(),
-                               contextInfo.getEntriesArray(), contextInfo.getStringsArray());
+               LttngLog4j2Api.tracepointWithContext(message, loggername, classname, methodname, filename, line,
+                               event.getTimeMillis(), event.getLevel().intLevel(), event.getThreadName(),
+                               contextInfo.getEntriesArray(), contextInfo.getStringsArray(), agent.getDomain() == Domain.LOG4J);
        }
 }
This page took 0.025723 seconds and 4 git commands to generate.