Add LOG4J2 domain to the Log4j 2.x agent
[lttng-ust.git] / src / lib / lttng-ust-java-agent / jni / log4j / lttng_ust_log4j2.c
index e89602f6a58685e4cf2c3ef33a1244c95f5da699..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"
 
 /*
@@ -40,12 +41,34 @@ enum loglevel_log4j2 {
         LOGLEVEL_LOG4J2_ALL              = INT32_MAX,
 };
 
+/*
+ * Convert a custom Log4j 2.x loglevel to its equivalent 1.x standard loglevel.
+ */
+static jint loglevel_custom_2x_to_standard_1x(jint loglevel)
+{
+       if (loglevel <= LOGLEVEL_LOG4J2_OFF) {
+               return LOGLEVEL_LOG4J1_OFF;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_FATAL) {
+               return LOGLEVEL_LOG4J1_FATAL;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_ERROR) {
+               return LOGLEVEL_LOG4J1_ERROR;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_WARN) {
+               return LOGLEVEL_LOG4J1_WARN;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_INFO) {
+               return LOGLEVEL_LOG4J1_INFO;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_DEBUG) {
+               return LOGLEVEL_LOG4J1_DEBUG;
+       } else if (loglevel <= LOGLEVEL_LOG4J2_TRACE) {
+               return LOGLEVEL_LOG4J1_TRACE;
+       } else {
+               return LOGLEVEL_LOG4J1_ALL;
+       }
+}
+
 /*
  * The integer values of the loglevels has obviously changed in log4j2,
  * translate them to the values of log4j1 since they are exposed in the API of
  * lttng-tools.
- *
- * Custom loglevels might pose a problem when using ranges.
  */
 static jint loglevel_2x_to_1x(jint loglevel)
 {
@@ -68,7 +91,7 @@ static jint loglevel_2x_to_1x(jint loglevel)
                return LOGLEVEL_LOG4J1_ALL;
        default:
                /* Handle custom loglevels. */
-               return loglevel;
+               return loglevel_custom_2x_to_standard_1x(loglevel);
        }
 }
 
@@ -87,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);
@@ -110,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;
This page took 0.024627 seconds and 4 git commands to generate.