X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=doc%2Fexamples%2Fjava-log4j2-ctx%2FHelloLog4j2Ctx.java;fp=doc%2Fexamples%2Fjava-log4j2-ctx%2FHelloLog4j2Ctx.java;h=b953223106ac0a0143bc2468ee73749f761caa6b;hb=464c475658ae29039b3b9e769b5b02195688a94a;hp=0000000000000000000000000000000000000000;hpb=08c1dfc43e014a99cd47202f6b370f8afa33e4c8;p=lttng-ust.git diff --git a/doc/examples/java-log4j2-ctx/HelloLog4j2Ctx.java b/doc/examples/java-log4j2-ctx/HelloLog4j2Ctx.java new file mode 100644 index 00000000..b9532231 --- /dev/null +++ b/doc/examples/java-log4j2-ctx/HelloLog4j2Ctx.java @@ -0,0 +1,71 @@ +/* + * SPDX-License-Identifier: MIT + * + * Copyright (C) 2022 EfficiOS Inc. + */ + +import java.net.URI; +import java.util.ArrayList; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LoggerContext; + +/** + * Example application using the LTTng-UST Java log4j agent. + * + *

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

+ * + * + * + */ +public class HelloLog4j2Ctx { + + /** + * Application start + * + * @param args Command-line arguments + */ + public static void main(String args[]) { + + URI configFileUri1 = URI.create("./log4j2.ctx1.xml"); + URI configFileUri2 = URI.create("./log4j2.ctx2.xml"); + + LoggerContext loggerContext1 = (LoggerContext) LogManager.getContext(ClassLoader.getSystemClassLoader(), false, + configFileUri1); + LoggerContext loggerContext2 = (LoggerContext) LogManager.getContext(ClassLoader.getSystemClassLoader(), false, + configFileUri2); + + /* Loggers in different contexts with the same name. */ + Logger logger1ctx1 = loggerContext1.getLogger(HelloLog4j2Ctx.class); + Logger logger1ctx2 = loggerContext2.getLogger(HelloLog4j2Ctx.class); + + Logger logger2ctx1 = loggerContext1.getLogger("Logger2"); + Logger logger3ctx2 = loggerContext2.getLogger("Logger3"); + + ArrayList loggers = new ArrayList(); + + loggers.add(logger1ctx1); + loggers.add(logger1ctx2); + loggers.add(logger2ctx1); + loggers.add(logger3ctx2); + + for (Logger logger : loggers) { + /* Trigger some tracing events using the Log4j Logger created before. */ + logger.info("Context config: Hello World, the answer is " + 42); + logger.info("Context config: Another info event"); + logger.error("Context config: An error event"); + } + } +}