2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2016-2022 EfficiOS Inc.
5 * Copyright (C) 2016 Alexandre Montplaisir <alexmonthy@efficios.com>
6 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include "org_lttng_ust_agent_log4j2_LttngLog4j2Api.h"
11 #include "lttng_ust_log4j_tp.h"
12 #include "lttng_ust_log4j2_tp.h"
13 #include "../common/lttng_ust_context.h"
16 * Those are an exact map from the class org.apache.log4j.Level.
18 enum loglevel_log4j1
{
19 LOGLEVEL_LOG4J1_OFF
= INT32_MAX
,
20 LOGLEVEL_LOG4J1_FATAL
= 50000,
21 LOGLEVEL_LOG4J1_ERROR
= 40000,
22 LOGLEVEL_LOG4J1_WARN
= 30000,
23 LOGLEVEL_LOG4J1_INFO
= 20000,
24 LOGLEVEL_LOG4J1_DEBUG
= 10000,
25 LOGLEVEL_LOG4J1_TRACE
= 5000,
26 LOGLEVEL_LOG4J1_ALL
= INT32_MIN
,
30 * Those are an exact map from the class
31 * org.apache.logging.log4j.spi.StandardLevel.
33 enum loglevel_log4j2
{
34 LOGLEVEL_LOG4J2_OFF
= 0,
35 LOGLEVEL_LOG4J2_FATAL
= 100,
36 LOGLEVEL_LOG4J2_ERROR
= 200,
37 LOGLEVEL_LOG4J2_WARN
= 300,
38 LOGLEVEL_LOG4J2_INFO
= 400,
39 LOGLEVEL_LOG4J2_DEBUG
= 500,
40 LOGLEVEL_LOG4J2_TRACE
= 600,
41 LOGLEVEL_LOG4J2_ALL
= INT32_MAX
,
45 * Convert a custom Log4j 2.x loglevel to its equivalent 1.x standard loglevel.
47 static jint
loglevel_custom_2x_to_standard_1x(jint loglevel
)
49 if (loglevel
<= LOGLEVEL_LOG4J2_OFF
) {
50 return LOGLEVEL_LOG4J1_OFF
;
51 } else if (loglevel
<= LOGLEVEL_LOG4J2_FATAL
) {
52 return LOGLEVEL_LOG4J1_FATAL
;
53 } else if (loglevel
<= LOGLEVEL_LOG4J2_ERROR
) {
54 return LOGLEVEL_LOG4J1_ERROR
;
55 } else if (loglevel
<= LOGLEVEL_LOG4J2_WARN
) {
56 return LOGLEVEL_LOG4J1_WARN
;
57 } else if (loglevel
<= LOGLEVEL_LOG4J2_INFO
) {
58 return LOGLEVEL_LOG4J1_INFO
;
59 } else if (loglevel
<= LOGLEVEL_LOG4J2_DEBUG
) {
60 return LOGLEVEL_LOG4J1_DEBUG
;
61 } else if (loglevel
<= LOGLEVEL_LOG4J2_TRACE
) {
62 return LOGLEVEL_LOG4J1_TRACE
;
64 return LOGLEVEL_LOG4J1_ALL
;
69 * The integer values of the loglevels has obviously changed in log4j2,
70 * translate them to the values of log4j1 since they are exposed in the API of
73 static jint
loglevel_2x_to_1x(jint loglevel
)
76 case LOGLEVEL_LOG4J2_OFF
:
77 return LOGLEVEL_LOG4J1_OFF
;
78 case LOGLEVEL_LOG4J2_FATAL
:
79 return LOGLEVEL_LOG4J1_FATAL
;
80 case LOGLEVEL_LOG4J2_ERROR
:
81 return LOGLEVEL_LOG4J1_ERROR
;
82 case LOGLEVEL_LOG4J2_WARN
:
83 return LOGLEVEL_LOG4J1_WARN
;
84 case LOGLEVEL_LOG4J2_INFO
:
85 return LOGLEVEL_LOG4J1_INFO
;
86 case LOGLEVEL_LOG4J2_DEBUG
:
87 return LOGLEVEL_LOG4J1_DEBUG
;
88 case LOGLEVEL_LOG4J2_TRACE
:
89 return LOGLEVEL_LOG4J1_TRACE
;
90 case LOGLEVEL_LOG4J2_ALL
:
91 return LOGLEVEL_LOG4J1_ALL
;
93 /* Handle custom loglevels. */
94 return loglevel_custom_2x_to_standard_1x(loglevel
);
99 * Tracepoint used by Java applications using the log4j 2.x handler.
101 JNIEXPORT
void JNICALL
Java_org_lttng_ust_agent_log4j2_LttngLog4j2Api_tracepointWithContext(JNIEnv
*env
,
102 jobject jobj
__attribute__((unused
)),
112 jbyteArray context_info_entries
,
113 jbyteArray context_info_strings
,
114 jboolean log4j1Compat
)
117 const char *msg_cstr
= (*env
)->GetStringUTFChars(env
, message
, &iscopy
);
118 const char *logger_name_cstr
= (*env
)->GetStringUTFChars(env
, loggerName
, &iscopy
);
119 const char *class_name_cstr
= (*env
)->GetStringUTFChars(env
, className
, &iscopy
);
120 const char *method_name_cstr
= (*env
)->GetStringUTFChars(env
, methodName
, &iscopy
);
121 const char *file_name_cstr
= (*env
)->GetStringUTFChars(env
, fileName
, &iscopy
);
122 const char *thread_name_cstr
= (*env
)->GetStringUTFChars(env
, threadName
, &iscopy
);
123 signed char *context_info_entries_array
;
124 signed char *context_info_strings_array
;
127 * Write these to the TLS variables, so that the UST callbacks in
128 * lttng_ust_context.c can access them.
130 context_info_entries_array
= (*env
)->GetByteArrayElements(env
, context_info_entries
, &iscopy
);
131 lttng_ust_context_info_tls
.ctx_entries
= (struct lttng_ust_jni_ctx_entry
*) context_info_entries_array
;
132 lttng_ust_context_info_tls
.ctx_entries_len
= (*env
)->GetArrayLength(env
, context_info_entries
);
133 context_info_strings_array
= (*env
)->GetByteArrayElements(env
, context_info_strings
, &iscopy
);
134 lttng_ust_context_info_tls
.ctx_strings
= context_info_strings_array
;
135 lttng_ust_context_info_tls
.ctx_strings_len
= (*env
)->GetArrayLength(env
, context_info_strings
);
139 * Log4j 1.x compatible tracepoint with loglevel conversion.
141 lttng_ust_tracepoint(lttng_log4j
, event
, msg_cstr
, logger_name_cstr
,
142 class_name_cstr
, method_name_cstr
, file_name_cstr
,
143 lineNumber
, timeStamp
, loglevel_2x_to_1x(logLevel
), thread_name_cstr
);
146 * Log4j 2.x tracepoint with native loglevel.
148 lttng_ust_tracepoint(lttng_log4j2
, event
, msg_cstr
, logger_name_cstr
,
149 class_name_cstr
, method_name_cstr
, file_name_cstr
,
150 lineNumber
, timeStamp
, logLevel
, thread_name_cstr
);
153 lttng_ust_context_info_tls
.ctx_entries
= NULL
;
154 lttng_ust_context_info_tls
.ctx_entries_len
= 0;
155 lttng_ust_context_info_tls
.ctx_strings
= NULL
;
156 lttng_ust_context_info_tls
.ctx_strings_len
= 0;
157 (*env
)->ReleaseStringUTFChars(env
, message
, msg_cstr
);
158 (*env
)->ReleaseStringUTFChars(env
, loggerName
, logger_name_cstr
);
159 (*env
)->ReleaseStringUTFChars(env
, className
, class_name_cstr
);
160 (*env
)->ReleaseStringUTFChars(env
, methodName
, method_name_cstr
);
161 (*env
)->ReleaseStringUTFChars(env
, fileName
, file_name_cstr
);
162 (*env
)->ReleaseStringUTFChars(env
, threadName
, thread_name_cstr
);
163 (*env
)->ReleaseByteArrayElements(env
, context_info_entries
, context_info_entries_array
, 0);
164 (*env
)->ReleaseByteArrayElements(env
, context_info_strings
, context_info_strings_array
, 0);