Commit | Line | Data |
---|---|---|
849202d4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: MIT |
849202d4 | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2015 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
5 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
849202d4 DG |
6 | */ |
7 | ||
d60dfbe4 AM |
8 | import java.io.IOException; |
9 | import java.util.logging.Handler; | |
849202d4 DG |
10 | import java.util.logging.Logger; |
11 | ||
d60dfbe4 | 12 | import org.lttng.ust.agent.jul.LttngLogHandler; |
849202d4 | 13 | |
5bfeaeca AM |
14 | /** |
15 | * Example application using the LTTng-UST Java JUL agent. | |
16 | * | |
d60dfbe4 AM |
17 | * <p> |
18 | * Basically all that is required is to instantiate a {@link LttngLogHandler} | |
19 | * and to attach it to a JUL {@link Logger}. Then use the Logger normally to log | |
20 | * messages, which will be sent to UST as trace events. | |
21 | * <p> | |
22 | * </p> | |
23 | * {@link Logger} names are used as event names on the UST side. This means that | |
24 | * by enabling/disabling certain events in the tracing session, you are | |
25 | * effectively enabling and disabling Loggers on the Java side. Note that this | |
26 | * applies only to {@link LttngLogHandler}'s. If other handlers are attached to | |
27 | * the Logger, those will continue logging events normally. | |
28 | * </p> | |
29 | * | |
1820ea46 AM |
30 | * <p> |
31 | * To obtain LTTng trace events, you should run the following sequence of | |
32 | * commands: | |
33 | * </p> | |
34 | * | |
35 | * <ul> | |
36 | * <li>$ lttng create</li> | |
37 | * <li>$ lttng enable-event -j -a</li> | |
38 | * <li>$ lttng start</li> | |
39 | * <li>(run this program)</li> | |
40 | * <li>$ lttng stop</li> | |
41 | * <li>$ lttng view</li> | |
42 | * <li>$ lttng destroy</li> | |
43 | * </ul> | |
44 | * | |
d60dfbe4 | 45 | * @author Alexandre Montplaisir |
5bfeaeca AM |
46 | * @author David Goulet |
47 | */ | |
48 | public class Hello { | |
49 | ||
d60dfbe4 AM |
50 | /** Class-wide JUL logger object */ |
51 | private static final Logger LOGGER = Logger.getLogger(Hello.class.getName()); | |
849202d4 | 52 | |
5bfeaeca AM |
53 | /** |
54 | * Application start | |
55 | * | |
56 | * @param args | |
57 | * Command-line arguments | |
d60dfbe4 | 58 | * @throws IOException |
1820ea46 AM |
59 | * If the required native libraries cannot be found. You may |
60 | * have to specify "-Djava.library.path=..." on the "java" | |
61 | * command line. | |
5bfeaeca | 62 | */ |
1820ea46 | 63 | public static void main(String args[]) throws IOException { |
849202d4 | 64 | |
d60dfbe4 AM |
65 | /* Instantiate a LTTngLogHandler object, and attach it to our logger */ |
66 | Handler lttngHandler = new LttngLogHandler(); | |
67 | LOGGER.addHandler(lttngHandler); | |
849202d4 | 68 | |
1820ea46 | 69 | /* Log events using the JUL Logger created before. */ |
d60dfbe4 | 70 | LOGGER.info("Hello World, the answer is " + 42); |
1820ea46 AM |
71 | LOGGER.info("Another info event"); |
72 | LOGGER.severe("A severe event"); | |
849202d4 | 73 | |
1820ea46 AM |
74 | /* Cleanup */ |
75 | LOGGER.removeHandler(lttngHandler); | |
d60dfbe4 | 76 | lttngHandler.close(); |
849202d4 DG |
77 | } |
78 | } |