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/7f64fe8c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/7f64fe8c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/7f64fe8c Branch: refs/heads/LOG4J-1181 Commit: 7f64fe8cbdfb672eab2fab280d5b676babc99087 Parents: 66ab53e Author: Ralph Goers <[email protected]> Authored: Sun Nov 29 11:37:04 2015 -0700 Committer: Ralph Goers <[email protected]> Committed: Sun Nov 29 11:37:04 2015 -0700 ---------------------------------------------------------------------- .../java/org/apache/log4j/LogWithRouteTest.java | 42 +++++++++++++++++ .../src/test/resources/log-RouteWithMDC.xml | 47 ++++++++++++++++++++ 2 files changed, 89 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7f64fe8c/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithRouteTest.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithRouteTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithRouteTest.java new file mode 100644 index 0000000..1a3b635 --- /dev/null +++ b/log4j-1.2-api/src/test/java/org/apache/log4j/LogWithRouteTest.java @@ -0,0 +1,42 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Class Description goes here. + * Created by rgoers on 11/29/15 + */ +public class LogWithRouteTest { + + private static final String CONFIG = "log-RouteWithMDC.xml"; + + @ClassRule + public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG); + + @Test + public void testMDC() throws Exception { + MDC.put("Type", "Service"); + MDC.put("Name", "John 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("Type is missing", msgs.get(0).contains("Type=Service")); + assertTrue("Name is missing", msgs.get(0).contains("Name=John Smith")); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7f64fe8c/log4j-1.2-api/src/test/resources/log-RouteWithMDC.xml ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/test/resources/log-RouteWithMDC.xml b/log4j-1.2-api/src/test/resources/log-RouteWithMDC.xml new file mode 100644 index 0000000..eb7e8a5 --- /dev/null +++ b/log4j-1.2-api/src/test/resources/log-RouteWithMDC.xml @@ -0,0 +1,47 @@ +<?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="%X{Type, Name} %m%n"/> + </Console> + <List name="List"> + <PatternLayout pattern="%X{Type, Name} %m%n"/> + </List> + <Routing name="Routing"> + <Routes pattern="$${ctx:Type}"> + <Route ref="STDOUT"/> + <Route ref="STDOUT" key="Audit"/> + <Route ref="List" key="Service"/> + </Routes> + </Routing> + </Appenders> + <Loggers> + <Logger name="org.apache.test.logging" level="debug" additivity="false"> + <AppenderRef ref="Routing"/> + </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
