|
| 1 | +package com.sap.hcp.cf.log4j2.filter; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.is; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.net.URL; |
| 7 | +import java.util.HashMap; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.Level; |
| 10 | +import org.apache.logging.log4j.core.Filter.Result; |
| 11 | +import org.apache.logging.log4j.core.LoggerContext; |
| 12 | +import org.apache.logging.log4j.core.appender.CountingNoOpAppender; |
| 13 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 14 | +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; |
| 15 | +import org.apache.logging.log4j.core.impl.JdkMapAdapterStringMap; |
| 16 | +import org.apache.logging.log4j.core.impl.Log4jLogEvent; |
| 17 | +import org.junit.Test; |
| 18 | +import org.slf4j.MDC; |
| 19 | + |
| 20 | +import com.sap.hcp.cf.logging.common.helper.DynamicLogLevelHelper; |
| 21 | + |
| 22 | +public class DynamicLevelPrefixLoggerFilterTest { |
| 23 | + |
| 24 | + private static final String KNOWN_PREFIX = "known.prefix"; |
| 25 | + private static final String UNKNOWN_PREFIX = "unknown.prefix"; |
| 26 | + |
| 27 | + private final JdkMapAdapterStringMap contextData; |
| 28 | + private final DynamicLevelPrefixLoggerFilter filter = new DynamicLevelPrefixLoggerFilter(); |
| 29 | + |
| 30 | + public DynamicLevelPrefixLoggerFilterTest() { |
| 31 | + HashMap<String, String> customMDC = new HashMap<>(); |
| 32 | + customMDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY, "DEBUG"); |
| 33 | + customMDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, KNOWN_PREFIX); |
| 34 | + this.contextData = new JdkMapAdapterStringMap(customMDC); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void acceptsOnKnownPackage() throws Exception { |
| 39 | + Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerFqcn(KNOWN_PREFIX + "acceptsOnKnownPackage") |
| 40 | + .setContextData(contextData).setLevel(Level.INFO).build(); |
| 41 | + assertThat(filter.filter(event), is(Result.ACCEPT)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void neutralOnUnknownPackage() throws Exception { |
| 46 | + Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerFqcn(UNKNOWN_PREFIX + "neutralOnUnknownPackage") |
| 47 | + .setContextData(contextData).setLevel(Level.INFO).build(); |
| 48 | + assertThat(filter.filter(event), is(Result.NEUTRAL)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void neutralOnLowerLevel() throws Exception { |
| 53 | + Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerFqcn(KNOWN_PREFIX + "neutralOnUnknownPackage") |
| 54 | + .setContextData(contextData).setLevel(Level.TRACE).build(); |
| 55 | + assertThat(filter.filter(event), is(Result.NEUTRAL)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void neutralOnUnconfiguredLevelKey() throws Exception { |
| 60 | + HashMap<String, String> customMDC = new HashMap<>(); |
| 61 | + customMDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, KNOWN_PREFIX); |
| 62 | + JdkMapAdapterStringMap missingLevelKey = new JdkMapAdapterStringMap(customMDC); |
| 63 | + Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerFqcn(KNOWN_PREFIX + "neutralOnUnknownPackage") |
| 64 | + .setContextData(missingLevelKey).setLevel(Level.INFO).build(); |
| 65 | + assertThat(filter.filter(event), is(Result.NEUTRAL)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void neutralOnUnconfiguredPrefix() throws Exception { |
| 70 | + HashMap<String, String> customMDC = new HashMap<>(); |
| 71 | + customMDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, KNOWN_PREFIX); |
| 72 | + JdkMapAdapterStringMap missingPrefixes = new JdkMapAdapterStringMap(customMDC); |
| 73 | + Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerFqcn(KNOWN_PREFIX + "neutralOnUnknownPackage") |
| 74 | + .setContextData(missingPrefixes).setLevel(Level.INFO).build(); |
| 75 | + assertThat(filter.filter(event), is(Result.NEUTRAL)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void integratesIntoConfiguration() throws Exception { |
| 80 | + |
| 81 | + LoggerContext loggerContext = new LoggerContext("integratesIntoConfiguration"); |
| 82 | + URL configLocation = getClass().getResource("log4j2-test.xml"); |
| 83 | + ConfigurationSource configurationSource = ConfigurationSource.fromUri(configLocation.toURI()); |
| 84 | + XmlConfiguration configuration = new XmlConfiguration(loggerContext, configurationSource); |
| 85 | + loggerContext.start(configuration); |
| 86 | + org.apache.logging.log4j.core.Logger logger = loggerContext |
| 87 | + .getLogger(KNOWN_PREFIX + "integratesIntoConfiguration"); |
| 88 | + CountingNoOpAppender appender = new CountingNoOpAppender("integratesIntoConfiguration", null); |
| 89 | + logger.addAppender(appender); |
| 90 | + logger.debug("test-integration-message-at-debug"); |
| 91 | + assertThat(appender.getCount(), is(0L)); |
| 92 | + logger.info("test-integration-message-at-info"); |
| 93 | + assertThat(appender.getCount(), is(1L)); |
| 94 | + MDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY, "DEBUG"); |
| 95 | + MDC.put(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES, KNOWN_PREFIX); |
| 96 | + logger.debug("test-integration-message-at-debug"); |
| 97 | + assertThat(appender.getCount(), is(2L)); |
| 98 | + loggerContext.close(); |
| 99 | + } |
| 100 | +} |
0 commit comments