| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2015 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 5 | * Copyright (C) 2013 David Goulet <dgoulet@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.jul.LttngLogHandler; |
| 13 | |
| 14 | /** |
| 15 | * Example application using the LTTng-UST Java JUL agent. |
| 16 | * |
| 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 | * |
| 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 | * |
| 45 | * @author Alexandre Montplaisir |
| 46 | * @author David Goulet |
| 47 | */ |
| 48 | public class Hello { |
| 49 | |
| 50 | /** Class-wide JUL logger object */ |
| 51 | private static final Logger LOGGER = Logger.getLogger(Hello.class.getName()); |
| 52 | |
| 53 | /** |
| 54 | * Application start |
| 55 | * |
| 56 | * @param args |
| 57 | * Command-line arguments |
| 58 | * @throws IOException |
| 59 | * If the required native libraries cannot be found. You may |
| 60 | * have to specify "-Djava.library.path=..." on the "java" |
| 61 | * command line. |
| 62 | */ |
| 63 | public static void main(String args[]) throws IOException { |
| 64 | |
| 65 | /* Instantiate a LTTngLogHandler object, and attach it to our logger */ |
| 66 | Handler lttngHandler = new LttngLogHandler(); |
| 67 | LOGGER.addHandler(lttngHandler); |
| 68 | |
| 69 | /* Log events using the JUL Logger created before. */ |
| 70 | LOGGER.info("Hello World, the answer is " + 42); |
| 71 | LOGGER.info("Another info event"); |
| 72 | LOGGER.severe("A severe event"); |
| 73 | |
| 74 | /* Cleanup */ |
| 75 | LOGGER.removeHandler(lttngHandler); |
| 76 | lttngHandler.close(); |
| 77 | } |
| 78 | } |