| 1 | /* |
| 2 | * Copyright (C) 2022, EfficiOS Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.utils; |
| 20 | |
| 21 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | import static org.junit.jupiter.api.Assertions.fail; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | |
| 26 | import org.apache.logging.log4j.Level; |
| 27 | import org.apache.logging.log4j.Logger; |
| 28 | import org.lttng.tools.ILttngSession.Domain; |
| 29 | import org.lttng.tools.LttngToolsHelper; |
| 30 | import org.lttng.ust.agent.log4j2.LttngLogAppender; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Utility methods for log4j 2.x tests |
| 35 | */ |
| 36 | public final class Log4j2TestUtils { |
| 37 | |
| 38 | private Log4j2TestUtils() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Setup method common to most log4j tests. To be called in a @BeforeClass. |
| 43 | * @param domain the tracing domain to operate on |
| 44 | */ |
| 45 | public static void testClassSetup(Domain domain) { |
| 46 | /* Make sure we can find the JNI library and lttng-tools */ |
| 47 | checkForLog4jLibrary(domain); |
| 48 | assertTrue(LttngUtils.checkForLttngTools(domain), "lttng-tools is not working properly."); |
| 49 | |
| 50 | LttngToolsHelper.destroyAllSessions(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Teardown method common to most log4j tests. To be called in a @AfterClass. |
| 55 | */ |
| 56 | public static void testClassCleanup() { |
| 57 | LttngToolsHelper.deleteAllTraces(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Check the the Log4j native library is available, effectively allowing |
| 62 | * LTTng Log4j appenders to be used. |
| 63 | */ |
| 64 | private static void checkForLog4jLibrary(Domain domain) { |
| 65 | try { |
| 66 | LttngLogAppender testAppender = LttngLogAppender.createAppender("checkForLttngTools", domain.toString(), null, null); |
| 67 | testAppender.close(); |
| 68 | } catch (SecurityException e) { |
| 69 | fail(e.getMessage()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Send 10 dummy events through the provided logger |
| 75 | * |
| 76 | * @param logger |
| 77 | * The logger to use to send events |
| 78 | */ |
| 79 | public static void send10Events(Logger logger) { |
| 80 | // Levels/priorities are DEBUG, ERROR, FATAL, INFO, TRACE, WARN |
| 81 | logger.debug("Debug message. Lost among so many."); |
| 82 | logger.debug("Debug message with a throwable", new IOException()); |
| 83 | logger.error("Error messsage. This might be bad."); |
| 84 | logger.error("Error message with a throwable", new IOException()); |
| 85 | logger.fatal("A fatal message. You are already dead."); |
| 86 | logger.info("A info message. Lol, who cares."); |
| 87 | logger.trace("A trace message. No, no *that* trace"); |
| 88 | logger.warn("A warn message. Yellow underline."); |
| 89 | logger.log(Level.DEBUG, "A debug message using .log()"); |
| 90 | logger.log(Level.ERROR, "A error message using .log()"); |
| 91 | } |
| 92 | } |