X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=doc%2Fexamples%2Fjava-log4j%2FHelloLog4j.java;fp=doc%2Fexamples%2Fjava-log4j%2FHelloLog4j.java;h=11c96ab5552a787be0c88feae0fa0cd48e2f7bdd;hb=464c475658ae29039b3b9e769b5b02195688a94a;hp=0000000000000000000000000000000000000000;hpb=08c1dfc43e014a99cd47202f6b370f8afa33e4c8;p=lttng-ust.git diff --git a/doc/examples/java-log4j/HelloLog4j.java b/doc/examples/java-log4j/HelloLog4j.java new file mode 100644 index 00000000..11c96ab5 --- /dev/null +++ b/doc/examples/java-log4j/HelloLog4j.java @@ -0,0 +1,89 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (C) 2015 EfficiOS Inc. + * Copyright (C) 2015 Alexandre Montplaisir + * Copyright (C) 2014 Christian Babeux + */ + +import java.io.IOException; + +import org.apache.log4j.Appender; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Logger; +import org.apache.log4j.Level; +import org.lttng.ust.agent.log4j.LttngLogAppender; + +/** + * Example application using the LTTng-UST Java log4j agent. + * + *

+ * To obtain LTTng trace events, you should run the following sequence of + * commands: + *

+ * + * + * + * @author Alexandre Montplaisir + * @author Christian Babeux + */ +public class HelloLog4j { + + private static final Logger HELLO_LOG = Logger.getLogger(HelloLog4j.class); + + /** + * Application start + * + * @param args + * Command-line arguments + * @throws IOException + * If the required native libraries cannot be found. You may + * have to specify "-Djava.library.path=..." on the "java" + * command line. + */ + public static void main(String args[]) throws IOException { + + /* + * Set lowest level to make sure all event levels are logged. + * Any jar can override the default log4j rootLogger level + * and a logger with no explicit level defaults to the non-null + * parent level. Events could be ignored if the inherited value + * is to low. + * e.g BSF -> https://issues.apache.org/jira/browse/BSF-24 + */ + HELLO_LOG.setLevel(Level.ALL); + + /* Start with the default Log4j configuration, which logs to console */ + BasicConfigurator.configure(); + + /* + * Instantiate a LTTng log appender and attach it to the logger, which + * will now send the logged events to UST. + */ + Appender lttngAppender = new LttngLogAppender(); + HELLO_LOG.addAppender(lttngAppender); + + /* + * Here we've set up the appender programmatically, but it could also be + * defined at runtime, by reading a configuration file for example: + */ + // PropertyConfigurator.configure(fileName); + + /* Trigger some tracing events using the Log4j Logger created before. */ + HELLO_LOG.info("Hello World, the answer is " + 42); + HELLO_LOG.info("Another info event"); + HELLO_LOG.error("An error event"); + + /* Cleanup */ + HELLO_LOG.removeAppender(lttngAppender); + lttngAppender.close(); + } +}