Move to kernel style SPDX license identifiers
[lttng-ust.git] / doc / examples / java-jul / ApplicationContextExample.java
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2016 EfficiOS Inc.
5 * Copyright (C) 2016 Alexandre Montplaisir <alexmonthy@efficios.com>
6 */
7
8 import java.io.IOException;
9 import java.util.logging.Handler;
10 import java.util.logging.Logger;
11
12 import org.lttng.ust.agent.context.ContextInfoManager;
13 import org.lttng.ust.agent.context.IContextInfoRetriever;
14 import org.lttng.ust.agent.jul.LttngLogHandler;
15
16 /**
17 * Example program defining a application context retriever, which allows
18 * attaching application-defined contexts to trace events.
19 *
20 * FIXME Use custom context names, and several names/types
21 *
22 * <p>
23 * Usage:
24 * <ul>
25 * <li>$ lttng create</li>
26 * <li>$ lttng enable-event -j -a</li>
27 * <li>$ lttng add-context -j -t '$app.myprovider:mystringcontext'</li>
28 * <li>$ lttng add-context -j -t '$app.myprovider:myshortcontext'</li>
29 * <li>$ lttng start</li>
30 * <li>(run this program)</li>
31 * <li>$ lttng stop</li>
32 * <li>$ lttng view</li>
33 * <li>$ lttng destroy</li>
34 * </ul>
35 * </p>
36 *
37 * The events present in the resulting trace should carry the context
38 * information defined in the example retriever.
39 *
40 * @author Alexandre Montplaisir
41 */
42 public class ApplicationContextExample {
43
44 /** Class-wide JUL logger object */
45 private static final Logger LOGGER = Logger.getLogger(ApplicationContextExample.class.getName());
46
47 private static final String RETRIEVER_NAME = "myprovider";
48 private static final String CONTEXT_NAME_STRING = "mystringcontext";
49 private static final String CONTEXT_NAME_SHORT = "myshortcontext";
50
51 private static class ExampleContextInfoRetriever implements IContextInfoRetriever {
52
53 @Override
54 public Object retrieveContextInfo(String key) {
55 if (CONTEXT_NAME_SHORT.equals(key)) {
56 return (short) 42;
57 } else if (CONTEXT_NAME_STRING.equals(key)) {
58 return "context-value!";
59 } else {
60 return null;
61 }
62 }
63
64 }
65
66 /**
67 * Application start
68 *
69 * @param args
70 * Command-line arguments
71 * @throws IOException
72 * @throws InterruptedException
73 */
74 public static void main(String args[]) throws IOException, InterruptedException {
75 /* Instantiate and attach a logger object */
76 Handler lttngHandler = new LttngLogHandler();
77 LOGGER.addHandler(lttngHandler);
78
79 /* Instantiate and register the context retriever */
80 IContextInfoRetriever cir = new ExampleContextInfoRetriever();
81 ContextInfoManager.getInstance().registerContextInfoRetriever(RETRIEVER_NAME, cir);
82
83 /*
84 * Make sure you have a LTTng session running with the appropriate
85 * events and contexts enabled! See the class Javadoc.
86 */
87
88 /* Trigger a tracing event using the JUL Logger created before. */
89 LOGGER.info("Log event #1");
90 LOGGER.warning("Log event #2");
91 LOGGER.severe("Log event #3");
92
93 /* Unregister our context retriever, and dispose the log handler */
94 ContextInfoManager.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME);
95 lttngHandler.close();
96 }
97 }
This page took 0.030209 seconds and 4 git commands to generate.