| 1 | /* |
| 2 | * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com> |
| 3 | * Copyright (C) 2014 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2014 Christian Babeux <christian.babeux@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | import java.io.IOException; |
| 11 | import java.lang.Integer; |
| 12 | |
| 13 | import org.apache.log4j.Appender; |
| 14 | import org.apache.log4j.BasicConfigurator; |
| 15 | import org.apache.log4j.Logger; |
| 16 | import org.apache.log4j.Level; |
| 17 | import org.lttng.ust.agent.log4j.LttngLogAppender; |
| 18 | |
| 19 | public class JTestLTTng { |
| 20 | |
| 21 | /** |
| 22 | * Application start |
| 23 | * |
| 24 | * @param args |
| 25 | * Command-line arguments |
| 26 | * @throws IOException |
| 27 | * @throws InterruptedException |
| 28 | */ |
| 29 | public static void main(String args[]) throws IOException, InterruptedException { |
| 30 | |
| 31 | Logger lttng = Logger.getLogger("log4j-event"); |
| 32 | Logger lttng2 = Logger.getLogger("log4j-event-2"); |
| 33 | |
| 34 | /* |
| 35 | * Set lowest level to make sure all event levels are logged. |
| 36 | * Any jar can override the default log4j rootLogger level |
| 37 | * and a logger with no explicit level defaults to the non-null |
| 38 | * parent level. Events could be ignored if the inherited value |
| 39 | * is too low, thereby failing the test. |
| 40 | * |
| 41 | * See BSF -> https://issues.apache.org/jira/browse/BSF-24 |
| 42 | */ |
| 43 | lttng.setLevel(Level.ALL); |
| 44 | lttng2.setLevel(Level.ALL); |
| 45 | |
| 46 | int nrIter = Integer.parseInt(args[0]); |
| 47 | int waitTime = Integer.parseInt(args[1]); |
| 48 | int fire_debug_tp = 0; |
| 49 | int fire_second_tp = 0; |
| 50 | |
| 51 | if (args.length > 2) { |
| 52 | fire_debug_tp = Integer.parseInt(args[2]); |
| 53 | } |
| 54 | if (args.length > 3) { |
| 55 | fire_second_tp = Integer.parseInt(args[3]); |
| 56 | } |
| 57 | |
| 58 | /* Start with the default Log4j configuration, which logs to console */ |
| 59 | BasicConfigurator.configure(); |
| 60 | |
| 61 | /* |
| 62 | * Add a LTTng log appender to both loggers, which will also send the |
| 63 | * logged events to UST. |
| 64 | */ |
| 65 | Appender lttngAppender = new LttngLogAppender(); |
| 66 | lttng.addAppender(lttngAppender); |
| 67 | lttng2.addAppender(lttngAppender); |
| 68 | |
| 69 | for (int iter = 0; iter < nrIter; iter++) { |
| 70 | lttng.info("LOG4J tp fired!"); |
| 71 | if (fire_debug_tp == 1) { |
| 72 | /* Third arg, trigger debug TP. */ |
| 73 | lttng.debug("LOG4J DEBUG tp fired"); |
| 74 | } |
| 75 | Thread.sleep(waitTime); |
| 76 | } |
| 77 | |
| 78 | if (fire_second_tp == 1) { |
| 79 | lttng2.info("LOG4J second logger fired"); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Do not forget to close() all handlers so that the agent can shutdown |
| 84 | * and the session daemon socket gets cleaned up explicitly. |
| 85 | */ |
| 86 | lttngAppender.close(); |
| 87 | } |
| 88 | } |