LOG4J2-937 - Verify MDC is included in LogEvent
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/66ab53ea Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/66ab53ea Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/66ab53ea Branch: refs/heads/LOG4J-1181 Commit: 66ab53eab95fd6420eaa4345e73a929b6c6c9729 Parents: b28682c Author: Ralph Goers <[email protected]> Authored: Sun Nov 29 10:15:47 2015 -0700 Committer: Ralph Goers <[email protected]> Committed: Sun Nov 29 10:15:47 2015 -0700 ---------------------------------------------------------------------- .../java/org/apache/log4j/LogWithMDCTest.java | 41 ++++++++++++++++++++ log4j-1.2-api/src/test/resources/logWithMDC.xml | 40 +++++++++++++++++++ 2 files changed, 81 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/66ab53ea/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithMDCTest.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithMDCTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithMDCTest.java new file mode 100644 index 0000000..bb3f9c2 --- /dev/null +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithMDCTest.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015 Nextiva, Inc. to Present. + * All rights reserved. + */ +package org.apache.log4j; + +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.ClassRule; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Class Description goes here. + * Created by rgoers on 11/29/15 + */ +public class LogWithMDCTest { + + private static final String CONFIG = "logWithMDC.xml"; + + @ClassRule + public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG); + + @Test + public void testMDC() throws Exception { + MDC.put("Key1", "John"); + MDC.put("Key2", "Smith"); + Logger logger = Logger.getLogger("org.apache.test.logging"); + logger.debug("This is a test"); + ListAppender listApp = (ListAppender) CTX.getAppender("List"); + assertNotNull(listApp); + List<String> msgs = listApp.getMessages(); + assertNotNull("No messages received", msgs); + assertTrue(msgs.size() == 1); + assertTrue("Key1 is missing", msgs.get(0).contains("Key1=John")); + assertTrue("Key2 is missing", msgs.get(0).contains("Key2=Smith")); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/66ab53ea/log4j-1.2-api/src/test/resources/logWithMDC.xml ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/resources/logWithMDC.xml b/log4j-1.2-api/src/test/resources/logWithMDC.xml new file mode 100644 index 0000000..1fe8482 --- /dev/null +++ b/log4j-1.2-api/src/test/resources/logWithMDC.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration name="ConfigTest" status="error" packages="org.apache.logging.log4j.test" + monitorInterval="5"> + <Appenders> + <Console name="STDOUT"> + <PatternLayout pattern="%m%n"/> + </Console> + <List name="List"> + <PatternLayout pattern="%X{Key1, Key2} %m%n"/> + </List> + </Appenders> + <Loggers> + <Logger name="org.apache.test.logging" level="debug" additivity="false"> + <AppenderRef ref="List"/> + </Logger> + <Logger name="org.apache.test" level="trace" additivity="false"> + <AppenderRef ref="List"/> + </Logger> + <Root level="error"> + <AppenderRef ref="STDOUT"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file
