fix: Convert custom loglevels in Log4j 2.x agent
authorMichael Jeanson <mjeanson@efficios.com>
Wed, 2 Feb 2022 19:04:50 +0000 (19:04 +0000)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 14 Feb 2022 16:27:32 +0000 (11:27 -0500)
The loglevel integer representation has changed between log4j 1.x and
2.x, we currently convert the standard loglevels but passthrough the
custom ones.

This can be problematic when using severity ranges as custom loglevels
won't be properly filtered.

Use the same strategy as the upstream Log4j 2.x compatibility layer by
converting the custom loglevels to their equivalent standard loglevel
value.

Change-Id: I8cbd4706cb774e334380050cf0b407e19d7bc7c4
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/lib/lttng-ust-java-agent/jni/log4j/lttng_ust_log4j2.c

index e89602f6a58685e4cf2c3ef33a1244c95f5da699..f01139ad473cd3c148d87cf67d98d1816612bef7 100644 (file)
@@ -40,12 +40,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 +90,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);
        }
 }
 
This page took 0.025522 seconds and 4 git commands to generate.