2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2016 EfficiOS Inc.
5 * Copyright (C) 2016 Alexandre Montplaisir <alexmonthy@efficios.com>
8 import java
.io
.IOException
;
9 import java
.util
.logging
.Handler
;
10 import java
.util
.logging
.Logger
;
12 import org
.lttng
.ust
.agent
.context
.ContextInfoManager
;
13 import org
.lttng
.ust
.agent
.context
.IContextInfoRetriever
;
14 import org
.lttng
.ust
.agent
.jul
.LttngLogHandler
;
17 * Example program defining a application context retriever, which allows
18 * attaching application-defined contexts to trace events.
20 * FIXME Use custom context names, and several names/types
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>
37 * The events present in the resulting trace should carry the context
38 * information defined in the example retriever.
40 * @author Alexandre Montplaisir
42 public class ApplicationContextExample
{
44 /** Class-wide JUL logger object */
45 private static final Logger LOGGER
= Logger
.getLogger(ApplicationContextExample
.class.getName());
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";
51 private static class ExampleContextInfoRetriever
implements IContextInfoRetriever
{
54 public Object
retrieveContextInfo(String key
) {
55 if (CONTEXT_NAME_SHORT
.equals(key
)) {
57 } else if (CONTEXT_NAME_STRING
.equals(key
)) {
58 return "context-value!";
70 * Command-line arguments
72 * @throws InterruptedException
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
);
79 /* Instantiate and register the context retriever */
80 IContextInfoRetriever cir
= new ExampleContextInfoRetriever();
81 ContextInfoManager
.getInstance().registerContextInfoRetriever(RETRIEVER_NAME
, cir
);
84 * Make sure you have a LTTng session running with the appropriate
85 * events and contexts enabled! See the class Javadoc.
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");
93 /* Unregister our context retriever, and dispose the log handler */
94 ContextInfoManager
.getInstance().unregisterContextInfoRetriever(RETRIEVER_NAME
);