| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2015 EfficiOS Inc. |
| 5 | * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com> |
| 6 | * Copyright (C) 2014 Christian Babeux <christian.babeux@efficios.com> |
| 7 | */ |
| 8 | |
| 9 | import java.io.IOException; |
| 10 | |
| 11 | import org.apache.log4j.Appender; |
| 12 | import org.apache.log4j.BasicConfigurator; |
| 13 | import org.apache.log4j.Logger; |
| 14 | import org.apache.log4j.Level; |
| 15 | import org.lttng.ust.agent.log4j.LttngLogAppender; |
| 16 | |
| 17 | /** |
| 18 | * Example application using the LTTng-UST Java log4j agent. |
| 19 | * |
| 20 | * <p> |
| 21 | * To obtain LTTng trace events, you should run the following sequence of |
| 22 | * commands: |
| 23 | * </p> |
| 24 | * |
| 25 | * <ul> |
| 26 | * <li>$ lttng create</li> |
| 27 | * <li>$ lttng enable-event -l -a</li> |
| 28 | * <li>$ lttng start</li> |
| 29 | * <li>(run this program)</li> |
| 30 | * <li>$ lttng stop</li> |
| 31 | * <li>$ lttng view</li> |
| 32 | * <li>$ lttng destroy</li> |
| 33 | * </ul> |
| 34 | * |
| 35 | * @author Alexandre Montplaisir |
| 36 | * @author Christian Babeux |
| 37 | */ |
| 38 | public class Hello { |
| 39 | |
| 40 | private static final Logger HELLO_LOG = Logger.getLogger(Hello.class); |
| 41 | |
| 42 | /** |
| 43 | * Application start |
| 44 | * |
| 45 | * @param args |
| 46 | * Command-line arguments |
| 47 | * @throws IOException |
| 48 | * If the required native libraries cannot be found. You may |
| 49 | * have to specify "-Djava.library.path=..." on the "java" |
| 50 | * command line. |
| 51 | */ |
| 52 | public static void main(String args[]) throws IOException { |
| 53 | |
| 54 | /* |
| 55 | * Set lowest level to make sure all event levels are logged. |
| 56 | * Any jar can override the default log4j rootLogger level |
| 57 | * and a logger with no explicit level defaults to the non-null |
| 58 | * parent level. Events could be ignored if the inherited value |
| 59 | * is to low. |
| 60 | * e.g BSF -> https://issues.apache.org/jira/browse/BSF-24 |
| 61 | */ |
| 62 | HELLO_LOG.setLevel(Level.ALL); |
| 63 | |
| 64 | /* Start with the default Log4j configuration, which logs to console */ |
| 65 | BasicConfigurator.configure(); |
| 66 | |
| 67 | /* |
| 68 | * Instantiate a LTTng log appender and attach it to the logger, which |
| 69 | * will now send the logged events to UST. |
| 70 | */ |
| 71 | Appender lttngAppender = new LttngLogAppender(); |
| 72 | HELLO_LOG.addAppender(lttngAppender); |
| 73 | |
| 74 | /* |
| 75 | * Here we've set up the appender programmatically, but it could also be |
| 76 | * defined at runtime, by reading a configuration file for example: |
| 77 | */ |
| 78 | // PropertyConfigurator.configure(fileName); |
| 79 | |
| 80 | /* Trigger some tracing events using the Log4j Logger created before. */ |
| 81 | HELLO_LOG.info("Hello World, the answer is " + 42); |
| 82 | HELLO_LOG.info("Another info event"); |
| 83 | HELLO_LOG.error("An error event"); |
| 84 | |
| 85 | /* Cleanup */ |
| 86 | HELLO_LOG.removeAppender(lttngAppender); |
| 87 | lttngAppender.close(); |
| 88 | } |
| 89 | } |