java-application: split
[lttng-docs.git] / contents / using-lttng / instrumenting / java-application / log4j.md
CommitLineData
215610eb
PP
1---
2id: log4j
3since: 2.6
4---
5
6LTTng features an Apache log4j 1.2 agent, which means your existing
7Java applications using log4j 1.2 for logging can record events to
8LTTng traces with just a minor source code modification.
9
10<div class="tip">
11<p>
12 <span class="t">Note:</span>This version of LTTng does not
13 support Log4j 2.
14</p>
15</div>
16
17Here's an example:
18
19~~~ java
20import org.apache.log4j.Logger;
21import org.apache.log4j.BasicConfigurator;
22import org.lttng.ust.agent.LTTngAgent;
23
24public class Test
25{
26 private static final int answer = 42;
27
28 public static void main(String[] argv) throws Exception
29 {
30 // create and configure a logger
31 Logger logger = Logger.getLogger(Test.class);
32 BasicConfigurator.configure();
33
34 // call this as soon as possible (before logging)
35 LTTngAgent lttngAgent = LTTngAgent.getLTTngAgent();
36
37 // log at will!
38 logger.info("some info");
39 logger.warn("some warning");
40 Thread.sleep(500);
41 logger.debug("debug information; the answer is " + answer);
42 Thread.sleep(123);
43 logger.error("error!");
44 logger.fatal("fatal error!");
45
46 // not mandatory, but cleaner
47 lttngAgent.dispose();
48 }
49}
50~~~
51
52To compile the snippet above, do:
53
54<pre class="term">
55javac -cp /usr/lib/lttng/java/liblttng-ust-agent.jar:$LOG4JCP Test.java
56</pre>
57
58where `$LOG4JCP` is the log4j 1.2 JAR file path.
59
60You can run the resulting compiled class like this:
61
62<pre class="term">
63java -cp /usr/lib/lttng/java/liblttng-ust-agent.jar:$LOG4JCP:. Test
64</pre>
This page took 0.024285 seconds and 4 git commands to generate.