Export the stream instance ID
[lttng-ust.git] / doc / examples / java-jul / Hello.java
CommitLineData
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
24import java.io.IOException;
25import java.util.logging.Handler;
849202d4
DG
26import java.util.logging.Logger;
27
d60dfbe4 28import 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 *
46 * @author Alexandre Montplaisir
5bfeaeca
AM
47 * @author David Goulet
48 */
49public class Hello {
50
d60dfbe4
AM
51 /** Class-wide JUL logger object */
52 private static final Logger LOGGER = Logger.getLogger(Hello.class.getName());
849202d4 53
5bfeaeca
AM
54 /**
55 * Application start
56 *
57 * @param args
58 * Command-line arguments
d60dfbe4
AM
59 * @throws IOException
60 * @throws InterruptedException
5bfeaeca 61 */
d60dfbe4 62 public static void main(String args[]) throws IOException, InterruptedException {
849202d4 63
d60dfbe4
AM
64 /* Instantiate a LTTngLogHandler object, and attach it to our logger */
65 Handler lttngHandler = new LttngLogHandler();
66 LOGGER.addHandler(lttngHandler);
849202d4
DG
67
68 /*
69 * Gives you time to do some lttng commands before any event is hit.
70 */
71 Thread.sleep(5000);
72
73 /* Trigger a tracing event using the JUL Logger created before. */
d60dfbe4 74 LOGGER.info("Hello World, the answer is " + 42);
849202d4
DG
75
76 /*
77 * From this point on, the above message will be collected in the trace
d60dfbe4 78 * if the event "Hello" is enabled for the JUL domain using the lttng
849202d4
DG
79 * command line or the lttng-ctl API. For instance:
80 *
d60dfbe4
AM
81 * $ lttng enable-event -j Hello
82 */
83
84 /*
85 * A new logger is created here and fired after. Typically with JUL, you
86 * use one static Logger per class. This example here can represent a
87 * class being lazy-loaded later in the execution of the application.
849202d4 88 *
d60dfbe4
AM
89 * The agent has an internal timer that is fired every 5 seconds in
90 * order to enable events that were not found at first but might need to
91 * be enabled when a new Logger appears. Unfortunately, there is no way
92 * right now to get notified of that so we have to actively poll.
849202d4
DG
93 *
94 * Using the --all command for instance, it will make this Logger
d60dfbe4
AM
95 * available in a LTTng trace after the internal agent's timer is fired.
96 * (lttng enable-event -j -a).
849202d4
DG
97 */
98 Logger helloLogDelayed = Logger.getLogger("hello_delay");
99
d60dfbe4
AM
100 /*
101 * Attach a handler to this new logger.
102 *
103 * Using the same handler as before would also work.
104 */
105 Handler lttngHandler2 = new LttngLogHandler();
106 helloLogDelayed.addHandler(lttngHandler2);
107
849202d4
DG
108 System.out.println("Firing hello delay in 10 seconds...");
109 Thread.sleep(10000);
110 helloLogDelayed.info("Hello World delayed...");
111
112 System.out.println("Cleaning Hello");
113
114 /*
d60dfbe4
AM
115 * Do not forget to close() all handlers so that the agent can shutdown
116 * and the session daemon socket gets cleaned up explicitly.
849202d4 117 */
d60dfbe4
AM
118 lttngHandler.close();
119 lttngHandler2.close();
849202d4
DG
120 }
121}
This page took 0.029409 seconds and 4 git commands to generate.