|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.test.rule; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
| 26 | +import org.apache.logging.log4j.Level; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import org.apache.logging.log4j.core.LoggerContext; |
| 29 | +import org.apache.logging.log4j.core.config.Configuration; |
| 30 | +import org.apache.logging.log4j.core.config.LoggerConfig; |
| 31 | +import org.junit.rules.MethodRule; |
| 32 | +import org.junit.runners.model.FrameworkMethod; |
| 33 | +import org.junit.runners.model.Statement; |
| 34 | + |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.util.ObjectUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * A JUnit method {@link org.junit.Rule} that changes the Log4J 2 logger level for a set of classes |
| 40 | + * or packages while a test method is running. Useful for performance or scalability tests |
| 41 | + * where we don't want to generate a large log in a tight inner loop, or |
| 42 | + * enabling debug logging for a test case. |
| 43 | + * |
| 44 | + * @author Artem Bilan |
| 45 | + * |
| 46 | + * @since 2.2 |
| 47 | + * |
| 48 | + */ |
| 49 | +public final class Log4j2LevelAdjuster implements MethodRule { |
| 50 | + |
| 51 | + private static final Log logger = LogFactory.getLog(Log4j2LevelAdjuster.class); |
| 52 | + |
| 53 | + private final Class<?>[] classes; |
| 54 | + |
| 55 | + private final Level level; |
| 56 | + |
| 57 | + private final String[] categories; |
| 58 | + |
| 59 | + private Log4j2LevelAdjuster(Level level) { |
| 60 | + this(level, null, new String[] { "org.springframework.integration" }); |
| 61 | + } |
| 62 | + |
| 63 | + private Log4j2LevelAdjuster(Level level, Class<?>[] classes, String[] categories) { |
| 64 | + Assert.notNull(level, "'level' must be null"); |
| 65 | + this.level = level; |
| 66 | + this.classes = classes != null ? classes : new Class<?>[0]; |
| 67 | + |
| 68 | + Stream<String> categoryStream = Stream.of(getClass().getPackage().getName()); |
| 69 | + |
| 70 | + if (!ObjectUtils.isEmpty(categories)) { |
| 71 | + categoryStream = Stream.concat(Arrays.stream(categories), categoryStream); |
| 72 | + } |
| 73 | + |
| 74 | + this.categories = categoryStream.toArray(String[]::new); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Statement apply(final Statement base, final FrameworkMethod method, Object target) { |
| 79 | + return new Statement() { |
| 80 | + |
| 81 | + @Override |
| 82 | + public void evaluate() throws Throwable { |
| 83 | + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); |
| 84 | + Configuration config = ctx.getConfiguration(); |
| 85 | + |
| 86 | + Map<Class<?>, Level> classLevels = new HashMap<>(); |
| 87 | + for (Class<?> cls : Log4j2LevelAdjuster.this.classes) { |
| 88 | + String className = cls.getName(); |
| 89 | + LoggerConfig loggerConfig = config.getLoggerConfig(className); |
| 90 | + LoggerConfig specificConfig = loggerConfig; |
| 91 | + |
| 92 | + // We need a specific configuration for this logger, |
| 93 | + // otherwise we would change the level of all other loggers |
| 94 | + // having the original configuration as parent as well |
| 95 | + |
| 96 | + if (!loggerConfig.getName().equals(className)) { |
| 97 | + specificConfig = new LoggerConfig(className, Log4j2LevelAdjuster.this.level, true); |
| 98 | + specificConfig.setParent(loggerConfig); |
| 99 | + config.addLogger(className, specificConfig); |
| 100 | + } |
| 101 | + |
| 102 | + classLevels.put(cls, specificConfig.getLevel()); |
| 103 | + specificConfig.setLevel(Log4j2LevelAdjuster.this.level); |
| 104 | + } |
| 105 | + |
| 106 | + Map<String, Level> categoryLevels = new HashMap<>(); |
| 107 | + for (String category : Log4j2LevelAdjuster.this.categories) { |
| 108 | + LoggerConfig loggerConfig = config.getLoggerConfig(category); |
| 109 | + LoggerConfig specificConfig = loggerConfig; |
| 110 | + |
| 111 | + // We need a specific configuration for this logger, |
| 112 | + // otherwise we would change the level of all other loggers |
| 113 | + // having the original configuration as parent as well |
| 114 | + |
| 115 | + if (!loggerConfig.getName().equals(category)) { |
| 116 | + specificConfig = new LoggerConfig(category, Log4j2LevelAdjuster.this.level, true); |
| 117 | + specificConfig.setParent(loggerConfig); |
| 118 | + config.addLogger(category, specificConfig); |
| 119 | + } |
| 120 | + |
| 121 | + categoryLevels.put(category, specificConfig.getLevel()); |
| 122 | + specificConfig.setLevel(Log4j2LevelAdjuster.this.level); |
| 123 | + } |
| 124 | + |
| 125 | + ctx.updateLoggers(); |
| 126 | + |
| 127 | + logger.debug("++++++++++++++++++++++++++++ " |
| 128 | + + "Overridden log level setting for: " + Arrays.toString(Log4j2LevelAdjuster.this.classes) |
| 129 | + + " and " + Arrays.toString(Log4j2LevelAdjuster.this.categories) |
| 130 | + + " for test " + method.getName()); |
| 131 | + |
| 132 | + try { |
| 133 | + base.evaluate(); |
| 134 | + } |
| 135 | + finally { |
| 136 | + logger.debug("++++++++++++++++++++++++++++ " |
| 137 | + + "Restoring log level setting for: " + Arrays.toString(Log4j2LevelAdjuster.this.classes) |
| 138 | + + " and " + Arrays.toString(Log4j2LevelAdjuster.this.categories) |
| 139 | + + " for test " + method.getName()); |
| 140 | + |
| 141 | + for (Class<?> cls : Log4j2LevelAdjuster.this.classes) { |
| 142 | + LoggerConfig loggerConfig = config.getLoggerConfig(cls.getName()); |
| 143 | + loggerConfig.setLevel(classLevels.get(cls)); |
| 144 | + } |
| 145 | + |
| 146 | + for (String category : Log4j2LevelAdjuster.this.categories) { |
| 147 | + LoggerConfig loggerConfig = config.getLoggerConfig(category); |
| 148 | + loggerConfig.setLevel(categoryLevels.get(category)); |
| 149 | + } |
| 150 | + |
| 151 | + ctx.updateLoggers(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Specify the classes for logging level adjusting configured before. |
| 160 | + * A new copy Log4j2LevelAdjuster instance is produced by this method. |
| 161 | + * The provided classes parameter overrides existing value in the {@link #classes}. |
| 162 | + * @param classes the classes to use for logging level adjusting |
| 163 | + * @return a Log4j2LevelAdjuster copy with the provided classes |
| 164 | + */ |
| 165 | + public Log4j2LevelAdjuster classes(Class<?>... classes) { |
| 166 | + return classes(false, classes); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Specify the classes for logging level adjusting configured before. |
| 171 | + * A new copy Log4j2LevelAdjuster instance is produced by this method. |
| 172 | + * The provided classes parameter can be merged with existing value in the {@link #classes}. |
| 173 | + * @param merge to merge or not with previously configured {@link #classes} |
| 174 | + * @param classes the classes to use for logging level adjusting |
| 175 | + * @return a Log4j2LevelAdjuster copy with the provided classes |
| 176 | + * @since 5.0.2 |
| 177 | + */ |
| 178 | + public Log4j2LevelAdjuster classes(boolean merge, Class<?>... classes) { |
| 179 | + return new Log4j2LevelAdjuster(this.level, |
| 180 | + merge ? Stream.of(this.classes, classes).flatMap(Stream::of).toArray(Class<?>[]::new) : classes, |
| 181 | + this.categories); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Specify the categories for logging level adjusting configured before. |
| 186 | + * A new copy Log4j2LevelAdjuster instance is produced by this method. |
| 187 | + * The provided categories parameter overrides existing value in the {@link #categories}. |
| 188 | + * @param categories the categories to use for logging level adjusting |
| 189 | + * @return a Log4j2LevelAdjuster copy with the provided categories |
| 190 | + */ |
| 191 | + public Log4j2LevelAdjuster categories(String... categories) { |
| 192 | + return categories(false, categories); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Specify the categories for logging level adjusting configured before. |
| 197 | + * A new copy Log4j2LevelAdjuster instance is produced by this method. |
| 198 | + * The provided categories parameter can be merged with existing value in the {@link #categories}. |
| 199 | + * @param merge to merge or not with previously configured {@link #categories} |
| 200 | + * @param categories the categories to use for logging level adjusting |
| 201 | + * @return a Log4j2LevelAdjuster copy with the provided categories |
| 202 | + * @since 5.0.2 |
| 203 | + */ |
| 204 | + public Log4j2LevelAdjuster categories(boolean merge, String... categories) { |
| 205 | + return new Log4j2LevelAdjuster(this.level, this.classes, |
| 206 | + merge ? Stream.of(this.categories, categories).flatMap(Stream::of).toArray(String[]::new) : categories); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * The factory to produce Log4j2LevelAdjuster instances for {@link Level#TRACE} logging |
| 211 | + * with the {@code org.springframework.integration} as default category. |
| 212 | + * @return the Log4j2LevelAdjuster instance |
| 213 | + */ |
| 214 | + public static Log4j2LevelAdjuster trace() { |
| 215 | + return forLevel(Level.TRACE); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * The factory to produce Log4j2LevelAdjuster instances for {@link Level#DEBUG} logging |
| 220 | + * with the {@code org.springframework.integration} as default category. |
| 221 | + * @return the Log4j2LevelAdjuster instance |
| 222 | + */ |
| 223 | + public static Log4j2LevelAdjuster debug() { |
| 224 | + return forLevel(Level.DEBUG); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * The factory to produce Log4j2LevelAdjuster instances for {@link Level#INFO} logging |
| 229 | + * with the {@code org.springframework.integration} as default category. |
| 230 | + * @return the Log4j2LevelAdjuster instance |
| 231 | + */ |
| 232 | + public static Log4j2LevelAdjuster info() { |
| 233 | + return forLevel(Level.INFO); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * The factory to produce Log4j2LevelAdjuster instances for arbitrary logging {@link Level} |
| 238 | + * with the {@code org.springframework.integration} as default category. |
| 239 | + * @param level the {@link Level} to use for logging |
| 240 | + * @return the Log4j2LevelAdjuster instance |
| 241 | + */ |
| 242 | + public static Log4j2LevelAdjuster forLevel(Level level) { |
| 243 | + return new Log4j2LevelAdjuster(level); |
| 244 | + } |
| 245 | + |
| 246 | +} |
0 commit comments