Repository: logging-log4j2 Updated Branches: refs/heads/master 56660bc7c -> f1e3bec24
Renamed InitialLoggerContext to LoggerContextRule. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f1e3bec2 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f1e3bec2 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f1e3bec2 Branch: refs/heads/master Commit: f1e3bec24c21527fd4e4bd34e7bc6ad8ae744cf2 Parents: 56660bc Author: ggregory <[email protected]> Authored: Wed Sep 23 20:09:45 2015 -0700 Committer: ggregory <[email protected]> Committed: Wed Sep 23 20:09:45 2015 -0700 ---------------------------------------------------------------------- .../logging/slf4j/CallerInformationTest.java | 130 ++++---- .../logging/slf4j/InitialLoggerContext.java | 71 ----- .../apache/logging/slf4j/LoggerContextRule.java | 72 +++++ .../org/apache/logging/slf4j/LoggerTest.java | 318 +++++++++---------- 4 files changed, 296 insertions(+), 295 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1e3bec2/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java ---------------------------------------------------------------------- diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java index e1d7d64..7ebab7a 100644 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java +++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java @@ -1,65 +1,65 @@ -/* - * 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. - */ -package org.apache.logging.slf4j; - -import java.util.List; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.testUtil.StringListAppender; -import org.apache.logging.log4j.LogManager; -import org.junit.ClassRule; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class CallerInformationTest { - - private static final String CONFIG = "target/test-classes/logback-calling-class.xml"; - - @ClassRule - public static final InitialLoggerContext CTX = new InitialLoggerContext(CONFIG); - - @Test - public void testClassLogger() throws Exception { - final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("ClassLogger"); - final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Class"); - logger.info("Ignored message contents."); - logger.warn("Verifying the caller class is still correct."); - logger.error("Hopefully nobody breaks me!"); - final List<String> messages = app.strList; - assertEquals("Incorrect number of messages.", 3, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller class name.", this.getClass().getName(), message); - } - } - - @Test - public void testMethodLogger() throws Exception { - final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("MethodLogger"); - final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Method"); - logger.info("More messages."); - logger.warn("CATASTROPHE INCOMING!"); - logger.error("ZOMBIES!!!"); - logger.warn("brains~~~"); - logger.info("Itchy. Tasty."); - final List<String> messages = app.strList; - assertEquals("Incorrect number of messages.", 5, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller method name.", "testMethodLogger", message); - } - } -} +/* + * 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. + */ +package org.apache.logging.slf4j; + +import java.util.List; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.testUtil.StringListAppender; +import org.apache.logging.log4j.LogManager; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CallerInformationTest { + + private static final String CONFIG = "target/test-classes/logback-calling-class.xml"; + + @ClassRule + public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG); + + @Test + public void testClassLogger() throws Exception { + final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("ClassLogger"); + final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Class"); + logger.info("Ignored message contents."); + logger.warn("Verifying the caller class is still correct."); + logger.error("Hopefully nobody breaks me!"); + final List<String> messages = app.strList; + assertEquals("Incorrect number of messages.", 3, messages.size()); + for (final String message : messages) { + assertEquals("Incorrect caller class name.", this.getClass().getName(), message); + } + } + + @Test + public void testMethodLogger() throws Exception { + final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("MethodLogger"); + final StringListAppender<ILoggingEvent> app = TestUtil.getListAppender(logger, "Method"); + logger.info("More messages."); + logger.warn("CATASTROPHE INCOMING!"); + logger.error("ZOMBIES!!!"); + logger.warn("brains~~~"); + logger.info("Itchy. Tasty."); + final List<String> messages = app.strList; + assertEquals("Incorrect number of messages.", 5, messages.size()); + for (final String message : messages) { + assertEquals("Incorrect caller method name.", "testMethodLogger", message); + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1e3bec2/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/InitialLoggerContext.java ---------------------------------------------------------------------- diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/InitialLoggerContext.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/InitialLoggerContext.java deleted file mode 100644 index 102e032..0000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/InitialLoggerContext.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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. - */ -package org.apache.logging.slf4j; - -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.joran.JoranConfigurator; -import ch.qos.logback.core.joran.GenericConfigurator; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import org.slf4j.LoggerFactory; - -/** - * JUnit {@link TestRule} similar to the TestRule in {@code log4j-core} of the same name. - * - * @since 2.1 - */ -public class InitialLoggerContext implements TestRule { - - private final String configLocation; - - private LoggerContext context; - - private String testClassName; - - public InitialLoggerContext(final String configLocation) { - this.configLocation = configLocation; - } - - @Override - public Statement apply(final Statement base, final Description description) { - testClassName = description.getClassName(); - return new Statement() { - @Override - public void evaluate() throws Throwable { - context = (LoggerContext) LoggerFactory.getILoggerFactory(); - final GenericConfigurator configurator = new JoranConfigurator(); - configurator.setContext(context); - configurator.doConfigure(configLocation); - base.evaluate(); - } - }; - } - - public LoggerContext getContext() { - return context; - } - - public Logger getLogger() { - return context.getLogger(testClassName); - } - - public Logger getLogger(final String name) { - return context.getLogger(name); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1e3bec2/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java ---------------------------------------------------------------------- diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java new file mode 100644 index 0000000..0f245e2 --- /dev/null +++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java @@ -0,0 +1,72 @@ +/* + * 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. + */ +package org.apache.logging.slf4j; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.joran.JoranConfigurator; +import ch.qos.logback.core.joran.GenericConfigurator; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import org.slf4j.LoggerFactory; + +/** + * JUnit {@link TestRule} similar to the TestRule in {@code log4j-core} of the same name. + * + * @since 2.1 + * @since 2.4.1 Renamed InitialLoggerContext to LoggerContextRule + */ +public class LoggerContextRule implements TestRule { + + private final String configLocation; + + private LoggerContext context; + + private String testClassName; + + public LoggerContextRule(final String configLocation) { + this.configLocation = configLocation; + } + + @Override + public Statement apply(final Statement base, final Description description) { + testClassName = description.getClassName(); + return new Statement() { + @Override + public void evaluate() throws Throwable { + context = (LoggerContext) LoggerFactory.getILoggerFactory(); + final GenericConfigurator configurator = new JoranConfigurator(); + configurator.setContext(context); + configurator.doConfigure(configLocation); + base.evaluate(); + } + }; + } + + public LoggerContext getContext() { + return context; + } + + public Logger getLogger() { + return context.getLogger(testClassName); + } + + public Logger getLogger(final String name) { + return context.getLogger(name); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f1e3bec2/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java ---------------------------------------------------------------------- diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java index b309edb..fb30a1b 100644 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java +++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java @@ -1,159 +1,159 @@ - -/* -* 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. -*/ -package org.apache.logging.slf4j; - -import java.util.Date; -import java.util.List; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.testUtil.StringListAppender; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.message.ParameterizedMessageFactory; -import org.apache.logging.log4j.message.StringFormatterMessageFactory; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -/** - * - */ -public class LoggerTest { - - private static final String CONFIG = "target/test-classes/logback-slf4j.xml"; - - @ClassRule - public static final InitialLoggerContext CTX = new InitialLoggerContext(CONFIG); - - private Logger logger; - private StringListAppender<ILoggingEvent> list; - - @Before - public void setUp() throws Exception { - final org.slf4j.Logger slf4jLogger = CTX.getLogger(); - logger = LogManager.getLogger(); - assertThat(slf4jLogger, is(theInstance(((SLF4JLogger) logger).getLogger()))); - final ch.qos.logback.classic.Logger rootLogger = CTX.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); - rootLogger.detachAppender("console"); - list = TestUtil.getListAppender(rootLogger, "LIST"); - assertThat(list, is(notNullValue())); - assertThat(list.strList, is(notNullValue())); - list.strList.clear(); - } - - @Test - public void basicFlow() { - logger.entry(); - logger.exit(); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void simpleFlow() { - logger.entry(CONFIG); - logger.exit(0); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void throwing() { - logger.throwing(new IllegalArgumentException("Test Exception")); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void catching() { - try { - throw new NullPointerException(); - } catch (final Exception e) { - logger.catching(e); - } - assertThat(list.strList, hasSize(1)); - } - - @Test - public void debug() { - logger.debug("Debug message"); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void getLogger_String_MessageFactoryMismatch() { - final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatch", - StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE); - testLogger.debug("%,d", Integer.MAX_VALUE); - assertThat(list.strList, hasSize(1)); - assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); - } - - @Test - public void getLogger_String_MessageFactoryMismatchNull() { - final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatchNull", - StringFormatterMessageFactory.INSTANCE, null); - testLogger.debug("%,d", Integer.MAX_VALUE); - assertThat(list.strList, hasSize(1)); - assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); - } - - private Logger testMessageFactoryMismatch(final String name, final MessageFactory messageFactory1, final MessageFactory messageFactory2) { - final Logger testLogger = LogManager.getLogger(name, messageFactory1); - assertThat(testLogger, is(notNullValue())); - assertThat(testLogger.getMessageFactory(), equalTo(messageFactory1)); - final Logger testLogger2 = LogManager.getLogger(name, messageFactory2); - assertThat(testLogger2.getMessageFactory(), equalTo(messageFactory1)); - return testLogger; - } - - @Test - public void debugObject() { - logger.debug(new Date()); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void debugWithParms() { - logger.debug("Hello, {}", "World"); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void testImpliedThrowable() { - logger.debug("This is a test", new Throwable("Testing")); - final List<String> msgs = list.strList; - assertThat(msgs, hasSize(1)); - final String expected = "java.lang.Throwable: Testing"; - assertTrue("Incorrect message data", msgs.get(0).contains(expected)); - } - - @SuppressWarnings("unchecked") - @Test - public void mdc() { - ThreadContext.put("TestYear", Integer.toString(2010)); - logger.debug("Debug message"); - ThreadContext.clearMap(); - logger.debug("Debug message"); - assertThat(list.strList, hasSize(2)); - assertTrue("Incorrect year", list.strList.get(0).startsWith("2010")); - } -} - + +/* +* 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. +*/ +package org.apache.logging.slf4j; + +import java.util.Date; +import java.util.List; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.testUtil.StringListAppender; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.message.MessageFactory; +import org.apache.logging.log4j.message.ParameterizedMessageFactory; +import org.apache.logging.log4j.message.StringFormatterMessageFactory; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * + */ +public class LoggerTest { + + private static final String CONFIG = "target/test-classes/logback-slf4j.xml"; + + @ClassRule + public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG); + + private Logger logger; + private StringListAppender<ILoggingEvent> list; + + @Before + public void setUp() throws Exception { + final org.slf4j.Logger slf4jLogger = CTX.getLogger(); + logger = LogManager.getLogger(); + assertThat(slf4jLogger, is(theInstance(((SLF4JLogger) logger).getLogger()))); + final ch.qos.logback.classic.Logger rootLogger = CTX.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); + rootLogger.detachAppender("console"); + list = TestUtil.getListAppender(rootLogger, "LIST"); + assertThat(list, is(notNullValue())); + assertThat(list.strList, is(notNullValue())); + list.strList.clear(); + } + + @Test + public void basicFlow() { + logger.entry(); + logger.exit(); + assertThat(list.strList, hasSize(2)); + } + + @Test + public void simpleFlow() { + logger.entry(CONFIG); + logger.exit(0); + assertThat(list.strList, hasSize(2)); + } + + @Test + public void throwing() { + logger.throwing(new IllegalArgumentException("Test Exception")); + assertThat(list.strList, hasSize(1)); + } + + @Test + public void catching() { + try { + throw new NullPointerException(); + } catch (final Exception e) { + logger.catching(e); + } + assertThat(list.strList, hasSize(1)); + } + + @Test + public void debug() { + logger.debug("Debug message"); + assertThat(list.strList, hasSize(1)); + } + + @Test + public void getLogger_String_MessageFactoryMismatch() { + final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatch", + StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE); + testLogger.debug("%,d", Integer.MAX_VALUE); + assertThat(list.strList, hasSize(1)); + assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); + } + + @Test + public void getLogger_String_MessageFactoryMismatchNull() { + final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatchNull", + StringFormatterMessageFactory.INSTANCE, null); + testLogger.debug("%,d", Integer.MAX_VALUE); + assertThat(list.strList, hasSize(1)); + assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); + } + + private Logger testMessageFactoryMismatch(final String name, final MessageFactory messageFactory1, final MessageFactory messageFactory2) { + final Logger testLogger = LogManager.getLogger(name, messageFactory1); + assertThat(testLogger, is(notNullValue())); + assertThat(testLogger.getMessageFactory(), equalTo(messageFactory1)); + final Logger testLogger2 = LogManager.getLogger(name, messageFactory2); + assertThat(testLogger2.getMessageFactory(), equalTo(messageFactory1)); + return testLogger; + } + + @Test + public void debugObject() { + logger.debug(new Date()); + assertThat(list.strList, hasSize(1)); + } + + @Test + public void debugWithParms() { + logger.debug("Hello, {}", "World"); + assertThat(list.strList, hasSize(1)); + } + + @Test + public void testImpliedThrowable() { + logger.debug("This is a test", new Throwable("Testing")); + final List<String> msgs = list.strList; + assertThat(msgs, hasSize(1)); + final String expected = "java.lang.Throwable: Testing"; + assertTrue("Incorrect message data", msgs.get(0).contains(expected)); + } + + @SuppressWarnings("unchecked") + @Test + public void mdc() { + ThreadContext.put("TestYear", Integer.toString(2010)); + logger.debug("Debug message"); + ThreadContext.clearMap(); + logger.debug("Debug message"); + assertThat(list.strList, hasSize(2)); + assertTrue("Incorrect year", list.strList.get(0).startsWith("2010")); + } +} +
