Commit | Line | Data |
---|---|---|
849202d4 | 1 | /* |
d60dfbe4 | 2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
849202d4 DG |
3 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to | |
7 | * deal in the Software without restriction, including without limitation the | |
8 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
9 | * sell copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | */ | |
23 | ||
d60dfbe4 AM |
24 | import java.io.IOException; |
25 | import java.util.logging.Handler; | |
849202d4 DG |
26 | import java.util.logging.Logger; |
27 | ||
d60dfbe4 | 28 | import org.lttng.ust.agent.jul.LttngLogHandler; |
849202d4 | 29 | |
5bfeaeca AM |
30 | /** |
31 | * Example application using the LTTng-UST Java JUL agent. | |
32 | * | |
d60dfbe4 AM |
33 | * <p> |
34 | * Basically all that is required is to instantiate a {@link LttngLogHandler} | |
35 | * and to attach it to a JUL {@link Logger}. Then use the Logger normally to log | |
36 | * messages, which will be sent to UST as trace events. | |
37 | * <p> | |
38 | * </p> | |
39 | * {@link Logger} names are used as event names on the UST side. This means that | |
40 | * by enabling/disabling certain events in the tracing session, you are | |
41 | * effectively enabling and disabling Loggers on the Java side. Note that this | |
42 | * applies only to {@link LttngLogHandler}'s. If other handlers are attached to | |
43 | * the Logger, those will continue logging events normally. | |
44 | * </p> | |
45 | * | |
1820ea46 AM |
46 | * <p> |
47 | * To obtain LTTng trace events, you should run the following sequence of | |
48 | * commands: | |
49 | * </p> | |
50 | * | |
51 | * <ul> | |
52 | * <li>$ lttng create</li> | |
53 | * <li>$ lttng enable-event -j -a</li> | |
54 | * <li>$ lttng start</li> | |
55 | * <li>(run this program)</li> | |
56 | * <li>$ lttng stop</li> | |
57 | * <li>$ lttng view</li> | |
58 | * <li>$ lttng destroy</li> | |
59 | * </ul> | |
60 | * | |
d60dfbe4 | 61 | * @author Alexandre Montplaisir |
5bfeaeca AM |
62 | * @author David Goulet |
63 | */ | |
64 | public class Hello { | |
65 | ||
d60dfbe4 AM |
66 | /** Class-wide JUL logger object */ |
67 | private static final Logger LOGGER = Logger.getLogger(Hello.class.getName()); | |
849202d4 | 68 | |
5bfeaeca AM |
69 | /** |
70 | * Application start | |
71 | * | |
72 | * @param args | |
73 | * Command-line arguments | |
d60dfbe4 | 74 | * @throws IOException |
1820ea46 AM |
75 | * If the required native libraries cannot be found. You may |
76 | * have to specify "-Djava.library.path=..." on the "java" | |
77 | * command line. | |
5bfeaeca | 78 | */ |
1820ea46 | 79 | public static void main(String args[]) throws IOException { |
849202d4 | 80 | |
d60dfbe4 AM |
81 | /* Instantiate a LTTngLogHandler object, and attach it to our logger */ |
82 | Handler lttngHandler = new LttngLogHandler(); | |
83 | LOGGER.addHandler(lttngHandler); | |
849202d4 | 84 | |
1820ea46 | 85 | /* Log events using the JUL Logger created before. */ |
d60dfbe4 | 86 | LOGGER.info("Hello World, the answer is " + 42); |
1820ea46 AM |
87 | LOGGER.info("Another info event"); |
88 | LOGGER.severe("A severe event"); | |
849202d4 | 89 | |
1820ea46 AM |
90 | /* Cleanup */ |
91 | LOGGER.removeHandler(lttngHandler); | |
d60dfbe4 | 92 | lttngHandler.close(); |
849202d4 DG |
93 | } |
94 | } |