Add LOG4J2 domain to the Log4j 2.x agent
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 10 Feb 2022 15:25:54 +0000 (15:25 +0000)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 14 Feb 2022 16:28:54 +0000 (11:28 -0500)
This commit adds a new LOG4J2 domain with native Log4j 2.x loglevels in
addition to the existing LOG4J domain. Both domains can be used
individually or at the same time by instantiating one or more appenders.

For example, a single appender in Log4j 2.x native mode:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
      <Appenders>
        <Lttng name="LTTNG" domain="LOG4J2"/>
      </Appenders>
      <Loggers>
        <Root level="all">
          <AppenderRef ref="LTTNG"/>
        </Root>
      </Loggers>
    </Configuration>

Two appenders to enable both domains:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
      <Appenders>
        <Lttng name="LTTNG1" domain="LOG4J"/>
        <Lttng name="LTTNG2" domain="LOG4J2"/>
      </Appenders>
      <Loggers>
        <Root level="all">
          <AppenderRef ref="LTTNG1"/>
          <AppenderRef ref="LTTNG2"/>
        </Root>
      </Loggers>
    </Configuration>

Change-Id: I9a41e107d19d5a0efffe055edf0ca5211a7f6d6b
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 files changed:
doc/examples/java-log4j2-basic/log4j2.xml
doc/examples/java-log4j2-ctx/log4j2.ctx1.xml
doc/examples/java-log4j2-ctx/log4j2.ctx2.xml
doc/examples/java-log4j2-prog/HelloLog4j2Prog.java
src/lib/lttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/ILttngAgent.java
src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLog4j2Agent.java
src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLog4j2Api.java
src/lib/lttng-ust-java-agent/java/lttng-ust-agent-log4j2/org/lttng/ust/agent/log4j2/LttngLogAppender.java
src/lib/lttng-ust-java-agent/jni/log4j/Makefile.am
src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2.c
src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.c [new file with mode: 0644]
src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.h [new file with mode: 0644]
src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j_tp.h

index 6ebcc3e14682785b1737665173a2a853ec053190..6e5fb83eaf95a0649c820413f5331a230fa981e1 100644 (file)
@@ -4,13 +4,15 @@
     <Console name="Console" target="SYSTEM_OUT">
       <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
     </Console>
-    <Lttng name="Lttng" domain="LOG4J">
+    <Lttng name="Lttng1" domain="LOG4J">
+    <Lttng name="Lttng2" domain="LOG4J2">
     </Lttng>
   </Appenders>
   <Loggers>
     <Root level="debug">
       <AppenderRef ref="Console"/>
-      <AppenderRef ref="Lttng"/>
+      <AppenderRef ref="Lttng1"/>
+      <AppenderRef ref="Lttng2"/>
     </Root>
   </Loggers>
 </Configuration>
index 52ad5a7ccbb08788e1d6a8543fc32ced20f8cc04..5606e452dbd5322a5d3a777a589cf531cd0ae267 100644 (file)
@@ -4,13 +4,15 @@
     <Console name="Console" target="SYSTEM_OUT">
       <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
     </Console>
-    <Lttng name="Lttng" domain="LOG4J">
+    <Lttng name="Lttng1" domain="LOG4J">
+    <Lttng name="Lttng2" domain="LOG4J2">
     </Lttng>
   </Appenders>
   <Loggers>
     <Root level="debug">
       <AppenderRef ref="Console"/>
-      <AppenderRef ref="Lttng"/>
+      <AppenderRef ref="Lttng1"/>
+      <AppenderRef ref="Lttng2"/>
     </Root>
   </Loggers>
 </Configuration>
index 011152dcb7ffb99f968c5977b8118f190e170995..003ae105bd365f2ac8b1f8af847a771ec39e7e49 100644 (file)
@@ -4,13 +4,15 @@
     <Console name="Console" target="SYSTEM_OUT">
       <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
     </Console>
-    <Lttng name="Lttng" domain="LOG4J">
+    <Lttng name="Lttng1" domain="LOG4J">
+    <Lttng name="Lttng2" domain="LOG4J2">
     </Lttng>
   </Appenders>
   <Loggers>
     <Root level="debug">
       <AppenderRef ref="Console"/>
-      <AppenderRef ref="Lttng"/>
+      <AppenderRef ref="Lttng1"/>
+      <AppenderRef ref="Lttng2"/>
     </Root>
   </Loggers>
 </Configuration>
index 2632ab1d7a2e32861c37e9ac012eef02526b4bd7..5b748cce858b3f72d2dcdba994c8b99afb6d23bd 100644 (file)
@@ -54,15 +54,21 @@ public class HelloLog4j2Prog {
                appenderBuilder.add(standardLayout);
                builder.add(appenderBuilder);
 
-               /* Create an Lttng appender */
+               /* Create an Lttng appender for the LOG4J domain */
                appenderBuilder = builder.newAppender("Lttng1", "Lttng");
                appenderBuilder.addAttribute("domain", "LOG4J");
                builder.add(appenderBuilder);
 
+               /* Create an Lttng appender for the LOG4J2 domain */
+               appenderBuilder = builder.newAppender("Lttng2", "Lttng");
+               appenderBuilder.addAttribute("domain", "LOG4J2");
+               builder.add(appenderBuilder);
+
                /* Create a root logger with both appenders attached */
                RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
                rootLogger.add(builder.newAppenderRef("Stdout"));
                rootLogger.add(builder.newAppenderRef("Lttng1"));
+               rootLogger.add(builder.newAppenderRef("Lttng2"));
                builder.add(rootLogger);
 
                Configurator.initialize(builder.build());
index ca2358a5f5e125fa4cfcbfb61ecca614e24de42f..ee0397c26a97a05a9bd8c1e93bef19e363dec5b1 100644 (file)
@@ -34,7 +34,7 @@ public interface ILttngAgent<T extends ILttngHandler> {
         * Tracing domains. Corresponds to domains defined by LTTng Tools.
         */
        enum Domain {
-               JUL(3), LOG4J(4);
+               JUL(3), LOG4J(4), LOG4J2(6);
                private int value;
 
                private Domain(int value) {
index cb7c35ad0ce8f5217fec9a413839e14fa98e9e86..707639a1f48babb6722e904767034cb39acaa573 100644 (file)
@@ -27,17 +27,25 @@ import org.lttng.ust.agent.AbstractLttngAgent;
  */
 class LttngLog4j2Agent extends AbstractLttngAgent<LttngLogAppender> {
 
-       private static LttngLog4j2Agent instance = null;
+       private static LttngLog4j2Agent log4j2_instance = null;
+       private static LttngLog4j2Agent log4j1_instance = null;
 
-       private LttngLog4j2Agent() {
-               super(Domain.LOG4J);
+       private LttngLog4j2Agent(Domain domain) {
+               super(domain);
        }
 
-       public static synchronized LttngLog4j2Agent getInstance() {
-               if (instance == null) {
-                       instance = new LttngLog4j2Agent();
+       public static synchronized LttngLog4j2Agent getLog4j1Instance() {
+               if (log4j1_instance == null) {
+                       log4j1_instance = new LttngLog4j2Agent(Domain.LOG4J);
                }
-               return instance;
+               return log4j1_instance;
+       }
+
+       public static synchronized LttngLog4j2Agent getLog4j2Instance() {
+               if (log4j2_instance == null) {
+                       log4j2_instance = new LttngLog4j2Agent(Domain.LOG4J2);
+               }
+               return log4j2_instance;
        }
 
        @Override
index 617d0d113af920968d74789649097df0d8f70538..bc907546852376b1abc1bd1f63d47d6d500f64c7 100644 (file)
@@ -17,5 +17,5 @@ final class LttngLog4j2Api {
 
        static native void tracepointWithContext(String message, String loggerName, String className, String methodName,
                        String fileName, int lineNumber, long timeStamp, int logLevel, String threadName, byte[] contextEntries,
-                       byte[] contextStrings);
-}
+                       byte[] contextStrings, boolean log4j1Compat);
+}
\ No newline at end of file
index 3b7afa5b72dadb4bd6510b42c94e423d402773d9..cebd18945e152cdb81ccc8461aa59e5a3b90eac3 100644 (file)
@@ -92,7 +92,9 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
 
                /* Register to the relevant agent. */
                if (domain == LttngLog4j2Agent.Domain.LOG4J) {
-                       agent = LttngLog4j2Agent.getInstance();
+                       agent = LttngLog4j2Agent.getLog4j1Instance();
+               } else if (domain == LttngLog4j2Agent.Domain.LOG4J2) {
+                       agent = LttngLog4j2Agent.getLog4j2Instance();
                } else {
                        throw new IllegalArgumentException("Unsupported domain '" + domain + "'");
                }
@@ -239,6 +241,6 @@ public final class LttngLogAppender extends AbstractAppender implements ILttngHa
 
                LttngLog4j2Api.tracepointWithContext(message, loggername, classname, methodname, filename, line,
                                event.getTimeMillis(), event.getLevel().intLevel(), event.getThreadName(),
-                               contextInfo.getEntriesArray(), contextInfo.getStringsArray());
+                               contextInfo.getEntriesArray(), contextInfo.getStringsArray(), agent.getDomain() == Domain.LOG4J);
        }
 }
index bc1db2484a959659e1ee3c6441cbfb69feccb9da..5e3fc938f725abb4a9052ad0a35ddf92083b0e83 100644 (file)
@@ -4,6 +4,8 @@ AM_CPPFLAGS += -I$(builddir) -I$(srcdir) $(JNI_CPPFLAGS)
 
 lib_LTLIBRARIES = liblttng-ust-log4j-jni.la
 liblttng_ust_log4j_jni_la_SOURCES = \
+       lttng_ust_log4j2_tp.c \
+       lttng_ust_log4j2_tp.h \
        lttng_ust_log4j_tp.c \
        lttng_ust_log4j_tp.h
 nodist_liblttng_ust_log4j_jni_la_SOURCES =
index f01139ad473cd3c148d87cf67d98d1816612bef7..d65dab7e0f90dacb851f4103820953c7b857befb 100644 (file)
@@ -9,6 +9,7 @@
 #define _LGPL_SOURCE
 #include "org_lttng_ust_agent_log4j2_LttngLog4j2Api.h"
 #include "lttng_ust_log4j_tp.h"
+#include "lttng_ust_log4j2_tp.h"
 #include "../common/lttng_ust_context.h"
 
 /*
@@ -109,7 +110,8 @@ JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j2_LttngLog4j2Api_tracepoint
                                                jint logLevel,
                                                jstring threadName,
                                                jbyteArray context_info_entries,
-                                               jbyteArray context_info_strings)
+                                               jbyteArray context_info_strings,
+                                               jboolean log4j1Compat)
 {
        jboolean iscopy;
        const char *msg_cstr = (*env)->GetStringUTFChars(env, message, &iscopy);
@@ -132,9 +134,21 @@ JNIEXPORT void JNICALL Java_org_lttng_ust_agent_log4j2_LttngLog4j2Api_tracepoint
        lttng_ust_context_info_tls.ctx_strings = context_info_strings_array;
        lttng_ust_context_info_tls.ctx_strings_len = (*env)->GetArrayLength(env, context_info_strings);
 
-       lttng_ust_tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr,
-                  class_name_cstr, method_name_cstr, file_name_cstr,
-                  lineNumber, timeStamp, loglevel_2x_to_1x(logLevel), thread_name_cstr);
+       if (log4j1Compat) {
+               /*
+                * Log4j 1.x compatible tracepoint with loglevel conversion.
+                */
+               lttng_ust_tracepoint(lttng_log4j, event, msg_cstr, logger_name_cstr,
+                          class_name_cstr, method_name_cstr, file_name_cstr,
+                          lineNumber, timeStamp, loglevel_2x_to_1x(logLevel), thread_name_cstr);
+       } else {
+               /*
+                * Log4j 2.x tracepoint with native loglevel.
+                */
+               lttng_ust_tracepoint(lttng_log4j2, event, msg_cstr, logger_name_cstr,
+                          class_name_cstr, method_name_cstr, file_name_cstr,
+                          lineNumber, timeStamp, logLevel, thread_name_cstr);
+       }
 
        lttng_ust_context_info_tls.ctx_entries = NULL;
        lttng_ust_context_info_tls.ctx_entries_len = 0;
diff --git a/src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.c b/src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.c
new file mode 100644 (file)
index 0000000..b905e81
--- /dev/null
@@ -0,0 +1,14 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2022 EfficiOS Inc.
+ */
+
+#define _LGPL_SOURCE
+
+#define LTTNG_UST_TRACEPOINT_HIDDEN_DEFINITION
+#define LTTNG_UST_TRACEPOINT_PROVIDER_HIDDEN_DEFINITION
+
+#define LTTNG_UST_TRACEPOINT_DEFINE
+#define LTTNG_UST_TRACEPOINT_CREATE_PROBES
+#include "lttng_ust_log4j2_tp.h"
diff --git a/src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.h b/src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2_tp.h
new file mode 100644 (file)
index 0000000..bbfe9b7
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2022 EfficiOS Inc.
+ */
+
+#undef LTTNG_UST_TRACEPOINT_PROVIDER
+#define LTTNG_UST_TRACEPOINT_PROVIDER lttng_log4j2
+
+#if !defined(_TRACEPOINT_LTTNG_UST_LOG4J2_H) || defined(LTTNG_UST_TRACEPOINT_HEADER_MULTI_READ)
+#define _TRACEPOINT_LTTNG_UST_LOG4J2_H
+
+#include <lttng/tracepoint.h>
+
+/*
+ * Tracepoint used by Java applications using the log4j 2.x convention for loglevels.
+ */
+LTTNG_UST_TRACEPOINT_EVENT(lttng_log4j2, event,
+       LTTNG_UST_TP_ARGS(
+               const char *, msg,
+               const char *, logger_name,
+               const char *, class_name,
+               const char *, method_name,
+               const char *, file_name,
+               int, line_number,
+               long, timestamp,
+               int, log_level,
+               const char *, thread_name),
+       LTTNG_UST_TP_FIELDS(
+               lttng_ust_field_string(msg, msg)
+               lttng_ust_field_string(logger_name, logger_name)
+               lttng_ust_field_string(class_name, class_name)
+               lttng_ust_field_string(method_name, method_name)
+               lttng_ust_field_string(filename, file_name)
+               lttng_ust_field_integer(int, line_number, line_number)
+               lttng_ust_field_integer(long, timestamp, timestamp)
+               lttng_ust_field_integer(int, int_loglevel, log_level)
+               lttng_ust_field_string(thread_name, thread_name)
+       )
+)
+
+#endif /* _TRACEPOINT_LTTNG_UST_LOG4J2_H */
+
+#undef LTTNG_UST_TRACEPOINT_INCLUDE
+#define LTTNG_UST_TRACEPOINT_INCLUDE "./lttng_ust_log4j2_tp.h"
+
+/* This part must be outside protection */
+#include <lttng/tracepoint-event.h>
index 8fa811871fbfe445ed300366043161f600f2bdb9..ce4f919d6d1e2fb37598d5c310c6ba7ec56294f0 100644 (file)
@@ -13,7 +13,7 @@
 #include <lttng/tracepoint.h>
 
 /*
- * Tracepoint used by Java applications using the log4j log appender.
+ * Tracepoint used by Java applications using the log4j 1.x convention for loglevels.
  */
 LTTNG_UST_TRACEPOINT_EVENT(lttng_log4j, event,
        LTTNG_UST_TP_ARGS(
This page took 0.032032 seconds and 4 git commands to generate.