2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
6 * Copyright (C) 2014 Christian Babeux <christian.babeux@efficios.com>
9 import java
.io
.IOException
;
11 import org
.apache
.log4j
.Appender
;
12 import org
.apache
.log4j
.BasicConfigurator
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.apache
.log4j
.Level
;
15 import org
.lttng
.ust
.agent
.log4j
.LttngLogAppender
;
18 * Example application using the LTTng-UST Java log4j agent.
21 * To obtain LTTng trace events, you should run the following sequence of
26 * <li>$ lttng create</li>
27 * <li>$ lttng enable-event -l -a</li>
28 * <li>$ lttng start</li>
29 * <li>(run this program)</li>
30 * <li>$ lttng stop</li>
31 * <li>$ lttng view</li>
32 * <li>$ lttng destroy</li>
35 * @author Alexandre Montplaisir
36 * @author Christian Babeux
40 private static final Logger HELLO_LOG
= Logger
.getLogger(Hello
.class);
46 * Command-line arguments
48 * If the required native libraries cannot be found. You may
49 * have to specify "-Djava.library.path=..." on the "java"
52 public static void main(String args
[]) throws IOException
{
55 * Set lowest level to make sure all event levels are logged.
56 * Any jar can override the default log4j rootLogger level
57 * and a logger with no explicit level defaults to the non-null
58 * parent level. Events could be ignored if the inherited value
60 * e.g BSF -> https://issues.apache.org/jira/browse/BSF-24
62 HELLO_LOG
.setLevel(Level
.ALL
);
64 /* Start with the default Log4j configuration, which logs to console */
65 BasicConfigurator
.configure();
68 * Instantiate a LTTng log appender and attach it to the logger, which
69 * will now send the logged events to UST.
71 Appender lttngAppender
= new LttngLogAppender();
72 HELLO_LOG
.addAppender(lttngAppender
);
75 * Here we've set up the appender programmatically, but it could also be
76 * defined at runtime, by reading a configuration file for example:
78 // PropertyConfigurator.configure(fileName);
80 /* Trigger some tracing events using the Log4j Logger created before. */
81 HELLO_LOG
.info("Hello World, the answer is " + 42);
82 HELLO_LOG
.info("Another info event");
83 HELLO_LOG
.error("An error event");
86 HELLO_LOG
.removeAppender(lttngAppender
);
87 lttngAppender
.close();
This page took 0.05244 seconds and 4 git commands to generate.