001// Generated by delombok at Fri Mar 03 17:40:07 UTC 2023 002package de.cuioss.test.juli; 003 004import static de.cuioss.tools.string.MoreStrings.isEmpty; 005import java.util.Arrays; 006import java.util.List; 007import java.util.Map.Entry; 008import java.util.Optional; 009import java.util.logging.ConsoleHandler; 010import java.util.logging.Handler; 011import java.util.logging.LogManager; 012import java.util.logging.Logger; 013// owolff: Changing the logger is the actual idea of this type, not 014// a security issue 015/** 016 * Central entry point for handling {@link TestLogHandler} 017 * 018 * 019 * @author Oliver Wolff 020 */ 021@SuppressWarnings("java:S4792") 022public final class TestLoggerFactory { 023 private static final StaticLoggerConfigurator configuration = new StaticLoggerConfigurator(); 024 private static final ConsoleHandlerModifier CONSOLE_HANDLER = new ConsoleHandlerModifier(); 025 026 /** 027 * Adds a {@link TestLogHandler} instance to jul's root logger. This method is reentrant, it 028 * ensures the {@link TestLogHandler} is installed only once 029 */ 030 public static void install() { 031 if (getTestHandlerOption().isEmpty()) { 032 CONSOLE_HANDLER.saveLevel(); 033 getRootLogger().addHandler(new TestLogHandler()); 034 } 035 } 036 037 /** 038 * Removes previously installed {@link TestLogHandler} instance and restores the previously 039 * stored {@link ConsoleHandler#getLevel()}. See also 040 * {@link #install()}. 041 */ 042 public static void uninstall() { 043 CONSOLE_HANDLER.restoreLevel(); 044 var testHandlerOption = getTestHandlerOption(); 045 testHandlerOption.ifPresent(testLogHandler -> getRootLogger().removeHandler(testLogHandler)); 046 } 047 048 /** 049 * Configures the logger sub-system according to the configuration found within 050 * {@link System#getProperties()} and / or the file "cui_logger.properties" usually located 051 * directly in "src/test/resources". 052 */ 053 public static void configureLogger() { 054 // Set Root logger 055 var rootLevel = configuration.getRootLevel(); 056 rootLevel.setAsRootLevel(); 057 CONSOLE_HANDLER.adjustLevel(rootLevel); 058 // Set concrete logger 059 for (Entry<String, TestLogLevel> entry : configuration.getConfiguredLogger().entrySet()) { 060 entry.getValue().addLogger(entry.getKey()); 061 } 062 } 063 064 private static Logger getRootLogger() { 065 return LogManager.getLogManager().getLogger(""); 066 } 067 068 private static List<Handler> getHandler() { 069 return Arrays.asList(getRootLogger().getHandlers()); 070 } 071 072 /** 073 * @return the configured {@link TestLogHandler} 074 * @throws AssertionError in case no {@link TestLogHandler} could be found. This is usually the 075 * case if {@link #install()} was not called prior to this request 076 */ 077 public static TestLogHandler getTestHandler() { 078 return getTestHandlerOption().orElseThrow(() -> new AssertionError("Unable to access de.cuioss.test.juli.TestLogHandler. Used properly?")); 079 } 080 081 /** 082 * @return the configured {@link TestLogHandler} if present 083 */ 084 public static Optional<TestLogHandler> getTestHandlerOption() { 085 for (Handler handler : getHandler()) { 086 if (handler instanceof TestLogHandler) { 087 return Optional.of((TestLogHandler) handler); 088 } 089 } 090 return Optional.empty(); 091 } 092 093 /** 094 * Convenient method for setting a Log-Level in context of the given {@link TestLogLevel} 095 * 096 * @param logLevel to be set 097 * @param loggerName if it is {@code null} or empty it will set the root-logger for the actual 098 * Log-Level 099 */ 100 public static void addLogger(TestLogLevel logLevel, String loggerName) { 101 CONSOLE_HANDLER.adjustLevel(logLevel); 102 if (isEmpty(loggerName)) { 103 Logger.getLogger("").setLevel(logLevel.getJuliLevel()); 104 } 105 Logger.getLogger(loggerName).setLevel(logLevel.getJuliLevel()); 106 } 107 108 @java.lang.SuppressWarnings("all") 109 @lombok.Generated 110 private TestLoggerFactory() { 111 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 112 } 113}