| 1 | /* |
| 2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * Copyright (C) 2014 - Christian Babeux <christian.babeux@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 | |
| 24 | import java.io.IOException; |
| 25 | |
| 26 | import org.apache.log4j.Appender; |
| 27 | import org.apache.log4j.BasicConfigurator; |
| 28 | import org.apache.log4j.Logger; |
| 29 | import org.lttng.ust.agent.log4j.LttngLogAppender; |
| 30 | |
| 31 | /** |
| 32 | * Example application using the LTTng-UST Java log4j agent. |
| 33 | * |
| 34 | * <p> |
| 35 | * To obtain LTTng trace events, you should run the following sequence of |
| 36 | * commands: |
| 37 | * </p> |
| 38 | * |
| 39 | * <ul> |
| 40 | * <li>$ lttng create</li> |
| 41 | * <li>$ lttng enable-event -l -a</li> |
| 42 | * <li>$ lttng start</li> |
| 43 | * <li>(run this program)</li> |
| 44 | * <li>$ lttng stop</li> |
| 45 | * <li>$ lttng view</li> |
| 46 | * <li>$ lttng destroy</li> |
| 47 | * </ul> |
| 48 | * |
| 49 | * @author Alexandre Montplaisir |
| 50 | * @author Christian Babeux |
| 51 | */ |
| 52 | public class Hello { |
| 53 | |
| 54 | private static final Logger HELLO_LOG = Logger.getLogger(Hello.class); |
| 55 | |
| 56 | /** |
| 57 | * Application start |
| 58 | * |
| 59 | * @param args |
| 60 | * Command-line arguments |
| 61 | * @throws IOException |
| 62 | * If the required native libraries cannot be found. You may |
| 63 | * have to specify "-Djava.library.path=..." on the "java" |
| 64 | * command line. |
| 65 | */ |
| 66 | public static void main(String args[]) throws IOException { |
| 67 | /* Start with the default Log4j configuration, which logs to console */ |
| 68 | BasicConfigurator.configure(); |
| 69 | |
| 70 | /* |
| 71 | * Instantiate a LTTng log appender and attach it to the logger, which |
| 72 | * will now send the logged events to UST. |
| 73 | */ |
| 74 | Appender lttngAppender = new LttngLogAppender(); |
| 75 | HELLO_LOG.addAppender(lttngAppender); |
| 76 | |
| 77 | /* |
| 78 | * Here we've set up the appender programmatically, but it could also be |
| 79 | * defined at runtime, by reading a configuration file for example: |
| 80 | */ |
| 81 | // PropertyConfigurator.configure(fileName); |
| 82 | |
| 83 | /* Trigger some tracing events using the Log4j Logger created before. */ |
| 84 | HELLO_LOG.info("Hello World, the answer is " + 42); |
| 85 | HELLO_LOG.info("Another info event"); |
| 86 | HELLO_LOG.error("An error event"); |
| 87 | |
| 88 | /* Cleanup */ |
| 89 | HELLO_LOG.removeAppender(lttngAppender); |
| 90 | lttngAppender.close(); |
| 91 | } |
| 92 | } |