1 package org
.lttng
.ust
.agent
.integration
.log4j
;
3 import static org
.junit
.Assert
.assertEquals
;
4 import static org
.junit
.Assert
.assertNotNull
;
5 import static org
.junit
.Assert
.assertTrue
;
6 import static org
.junit
.Assert
.fail
;
7 import static org
.junit
.Assume
.assumeTrue
;
9 import java
.lang
.reflect
.Field
;
10 import java
.util
.List
;
12 import org
.apache
.log4j
.Level
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.junit
.After
;
15 import org
.junit
.AfterClass
;
16 import org
.junit
.Before
;
17 import org
.junit
.BeforeClass
;
18 import org
.junit
.Test
;
19 import org
.lttng
.ust
.agent
.ILttngHandler
;
20 import org
.lttng
.ust
.agent
.LTTngAgent
;
21 import org
.lttng
.ust
.agent
.utils
.LttngSession
;
22 import org
.lttng
.ust
.agent
.utils
.LttngSession
.Domain
;
23 import org
.lttng
.ust
.agent
.utils
.TestUtils
;
25 @SuppressWarnings("deprecation")
26 public class Log4jLegacyApiTest
{
28 private static final Domain DOMAIN
= Domain
.LOG4J
;
30 private static final String EVENT_NAME_A
= "EventA";
31 private static final String EVENT_NAME_B
= "EventB";
33 private LttngSession session
;
35 private Logger loggerA
;
36 private Logger loggerB
;
39 public static void classSetup() {
40 /* Skip tests if we can't find the JNI library or lttng-tools */
41 assumeTrue(TestUtils
.checkForLog4jLibrary());
42 assumeTrue(TestUtils
.checkForLttngTools(Domain
.LOG4J
));
46 public static void classCleanup() {
47 LttngSession
.deleteAllTracee();
52 loggerA
= Logger
.getLogger(EVENT_NAME_A
);
53 LTTngAgent
.getLTTngAgent();
54 loggerB
= Logger
.getLogger(EVENT_NAME_B
);
56 loggerA
.setLevel(Level
.ALL
);
57 loggerB
.setLevel(Level
.ALL
);
59 session
= new LttngSession(null, DOMAIN
);
63 public void tearDown() {
73 public void testNoEvents() {
74 assertTrue(session
.start());
76 Log4jTestUtils
.send10Events(loggerA
);
77 Log4jTestUtils
.send10Events(loggerB
);
79 assertTrue(session
.stop());
81 List
<String
> output
= session
.view();
82 assertNotNull(output
);
83 assertTrue(output
.isEmpty());
85 ILttngHandler handler
= getAgentHandler();
86 assertEquals(0, handler
.getEventCount());
90 public void testAllEvents() {
91 assertTrue(session
.enableAllEvents());
92 assertTrue(session
.start());
94 Log4jTestUtils
.send10Events(loggerA
);
95 Log4jTestUtils
.send10Events(loggerB
);
97 assertTrue(session
.stop());
99 List
<String
> output
= session
.view();
100 assertNotNull(output
);
101 assertEquals(20, output
.size());
103 ILttngHandler handler
= getAgentHandler();
104 assertEquals(20, handler
.getEventCount());
108 public void testSomeEvents() {
109 assertTrue(session
.enableEvents(EVENT_NAME_A
));
110 assertTrue(session
.start());
112 Log4jTestUtils
.send10Events(loggerA
);
113 Log4jTestUtils
.send10Events(loggerB
);
115 assertTrue(session
.stop());
117 List
<String
> output
= session
.view();
118 assertNotNull(output
);
119 assertEquals(10, output
.size());
121 ILttngHandler handler
= getAgentHandler();
122 assertEquals(10, handler
.getEventCount());
126 * Get the singleton Log4j Handler currently managed by the LTTngAgent. It
127 * is not public, so we need reflection to access it.
129 * @return The agent's Log4j handler
131 private static ILttngHandler
getAgentHandler() {
133 Field log4jAppenderField
= LTTngAgent
.class.getDeclaredField("log4jAppender");
134 log4jAppenderField
.setAccessible(true);
135 return (ILttngHandler
) log4jAppenderField
.get(LTTngAgent
.getLTTngAgent());
136 } catch (ReflectiveOperationException
| SecurityException e
) {