Repository: ignite Updated Branches: refs/heads/ignite-3478 1b4eb29ba -> 7bd6275de
IGNITE-6362 Fixed NPE in Log4J2Logger. Cleanup tests and added tests to suite. This closes #2833. Signed-off-by: nikolay_tikhonov <ntikho...@gridgain.com> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f8824c86 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f8824c86 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f8824c86 Branch: refs/heads/ignite-3478 Commit: f8824c86112c99f5f9657d86a3f0aa53777b1f17 Parents: 6679b6c Author: Alexey Popov <tank2.a...@gmail.com> Authored: Mon Oct 16 15:55:57 2017 +0300 Committer: nikolay_tikhonov <ntikho...@gridgain.com> Committed: Mon Oct 16 15:55:57 2017 +0300 ---------------------------------------------------------------------- .../ignite/logger/log4j2/Log4J2Logger.java | 130 ++++++++----------- .../log4j2/GridLog4j2CorrectFileNameTest.java | 94 -------------- .../log4j2/GridLog4j2InitializedTest.java | 77 ----------- .../log4j2/GridLog4j2LoggingFileTest.java | 68 ---------- .../logger/log4j2/Log4j2LoggerSelfTest.java | 7 + .../log4j2/Log4j2LoggerVerboseModeSelfTest.java | 71 ++++++---- .../testsuites/IgniteLog4j2TestSuite.java | 2 + 7 files changed, 108 insertions(+), 341 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java b/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java index 46215e5..5c92afa 100644 --- a/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java +++ b/modules/log4j2/src/main/java/org/apache/ignite/logger/log4j2/Log4J2Logger.java @@ -42,10 +42,8 @@ import org.apache.logging.log4j.core.appender.FileAppender; import org.apache.logging.log4j.core.appender.RollingFileAppender; import org.apache.logging.log4j.core.appender.routing.RoutingAppender; import org.apache.logging.log4j.core.config.AppenderControl; -import org.apache.logging.log4j.core.config.AppenderRef; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Configurator; -import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.layout.PatternLayout; import org.jetbrains.annotations.Nullable; @@ -102,11 +100,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") private Logger impl; - /** Auto added at verbose mode console logger (nullable). */ - @GridToStringExclude - @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") - private Logger consoleLog; - /** Quiet flag. */ private final boolean quiet; @@ -117,12 +110,15 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { * Creates new logger with given implementation. * * @param impl Log4j implementation to use. - * @param consoleLog Cosole logger (optional). */ - private Log4J2Logger(final Logger impl, @Nullable final Logger consoleLog) { + private Log4J2Logger(final Logger impl) { assert impl != null; - this.impl = impl; - this.consoleLog = consoleLog; + + addConsoleAppenderIfNeeded(new C1<Boolean, Logger>() { + @Override public Logger apply(Boolean init) { + return impl; + } + }); quiet = quiet0; } @@ -130,17 +126,17 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { /** * Creates new logger with given configuration {@code path}. * - * @param path Path to log4j configuration XML file. + * @param path Path to log4j2 configuration XML file. * @throws IgniteCheckedException Thrown in case logger can't be created. */ public Log4J2Logger(String path) throws IgniteCheckedException { if (path == null) - throw new IgniteCheckedException("Configuration XML file for Log4j must be specified."); + throw new IgniteCheckedException("Configuration XML file for Log4j2 must be specified."); final URL cfgUrl = U.resolveIgniteUrl(path); if (cfgUrl == null) - throw new IgniteCheckedException("Log4j configuration path was not found: " + path); + throw new IgniteCheckedException("Log4j2 configuration path was not found: " + path); addConsoleAppenderIfNeeded(new C1<Boolean, Logger>() { @Override public Logger apply(Boolean init) { @@ -204,18 +200,16 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { } /** - * Sets level for internal log4j implementation. - * - * @param level Log level to set. + * Cleans up the logger configuration. Should be used in unit tests only for sequential tests run with + * different configurations */ - public void setLevel(Level level) { - LoggerContext ctx = (LoggerContext)LogManager.getContext(false); - - Configuration conf = ctx.getConfiguration(); - - conf.getLoggerConfig(impl.getName()).setLevel(level); + static void cleanup() { + synchronized (mux) { + if (inited) + LogManager.shutdown(); - ctx.updateLoggers(conf); + inited = false; + } } /** {@inheritDoc} */ @@ -242,10 +236,10 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { Appender innerApp = control.getAppender(); if (innerApp instanceof FileAppender) - return normilize(((FileAppender)innerApp).getFileName()); + return normalize(((FileAppender)innerApp).getFileName()); if (innerApp instanceof RollingFileAppender) - return normilize(((RollingFileAppender)innerApp).getFileName()); + return normalize(((RollingFileAppender)innerApp).getFileName()); } } catch (IllegalAccessException | NoSuchFieldException e) { @@ -265,7 +259,7 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { * @param path Path. * @return Normalized path. */ - private String normilize(String path) { + private String normalize(String path) { if (!U.isWindows()) return path; @@ -335,7 +329,7 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { // User launched ignite in verbose mode and did not add console appender with INFO level // to configuration and did not set IGNITE_CONSOLE_APPENDER to false. - consoleLog = createConsoleLogger(); + createConsoleLogger(); } quiet0 = quiet; @@ -348,14 +342,13 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { * * @return Logger with auto configured console appender. */ - public static Logger createConsoleLogger() { - LoggerContext ctx = (LoggerContext)LogManager.getContext(true); + public Logger createConsoleLogger() { + // from http://logging.apache.org/log4j/2.x/manual/customconfig.html + final LoggerContext ctx = impl.getContext(); - Configuration cfg = ctx.getConfiguration(); + final Configuration cfg = ctx.getConfiguration(); - PatternLayout.Builder builder = PatternLayout.newBuilder(); - - builder + PatternLayout.Builder builder = PatternLayout.newBuilder() .withPattern("%d{ISO8601}][%-5p][%t][%c{1}] %m%n") .withCharset(Charset.defaultCharset()) .withAlwaysWriteExceptions(false) @@ -363,9 +356,7 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { PatternLayout layout = builder.build(); - ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder(); - - consoleAppenderBuilder + ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder() .withName(CONSOLE_APPENDER) .withLayout(layout); @@ -373,20 +364,12 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { consoleApp.start(); - AppenderRef ref = AppenderRef.createAppenderRef(CONSOLE_APPENDER, Level.TRACE, null); - - AppenderRef[] refs = {ref}; - - LoggerConfig logCfg = LoggerConfig.createLogger(false, Level.INFO, LogManager.ROOT_LOGGER_NAME, "", refs, null, null, null); - - logCfg.addAppender(consoleApp, null, null); cfg.addAppender(consoleApp); - - cfg.addLogger(LogManager.ROOT_LOGGER_NAME, logCfg); + cfg.getRootLogger().addAppender(consoleApp, Level.TRACE, null); ctx.updateLoggers(cfg); - return (Logger)LogManager.getContext().getLogger(LogManager.ROOT_LOGGER_NAME); + return ctx.getRootLogger(); } /** {@inheritDoc} */ @@ -398,7 +381,22 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { // Set nodeId as system variable to be used at configuration. System.setProperty(NODE_ID, U.id8(nodeId)); - ((LoggerContext)LogManager.getContext(false)).reconfigure(); + if (inited) { + final LoggerContext ctx = impl.getContext(); + + synchronized (mux) { + inited = false; + } + + addConsoleAppenderIfNeeded(new C1<Boolean, Logger>() { + @Override public Logger apply(Boolean init) { + if (init) + ctx.reconfigure(); + + return (Logger)LogManager.getRootLogger(); + } + }); + } } /** {@inheritDoc} */ @@ -417,20 +415,17 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { */ @Override public Log4J2Logger getLogger(Object ctgr) { if (ctgr == null) - return new Log4J2Logger((Logger)LogManager.getRootLogger(), - consoleLog == null ? null : (Logger)LogManager.getContext().getLogger("")); + return new Log4J2Logger((Logger)LogManager.getRootLogger()); if (ctgr instanceof Class) { String name = ((Class<?>)ctgr).getName(); - return new Log4J2Logger((Logger)LogManager.getLogger(name), - consoleLog == null ? null : (Logger)LogManager.getContext().getLogger(name)); + return new Log4J2Logger((Logger)LogManager.getLogger(name)); } String name = ctgr.toString(); - return new Log4J2Logger((Logger)LogManager.getLogger(name), - consoleLog == null ? null : (Logger)LogManager.getContext().getLogger(name)); + return new Log4J2Logger((Logger)LogManager.getLogger(name)); } /** {@inheritDoc} */ @@ -439,9 +434,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { warning("Logging at TRACE level without checking if TRACE level is enabled: " + msg); impl.trace(msg); - - if (consoleLog != null) - consoleLog.trace(msg); } /** {@inheritDoc} */ @@ -450,9 +442,6 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { warning("Logging at DEBUG level without checking if DEBUG level is enabled: " + msg); impl.debug(msg); - - if (consoleLog != null) - consoleLog.debug(msg); } /** {@inheritDoc} */ @@ -461,56 +450,41 @@ public class Log4J2Logger implements IgniteLogger, LoggerNodeIdAware { warning("Logging at INFO level without checking if INFO level is enabled: " + msg); impl.info(msg); - - if (consoleLog != null) - consoleLog.info(msg); } /** {@inheritDoc} */ @Override public void warning(String msg) { impl.warn(msg); - - if (consoleLog != null) - consoleLog.warn(msg); } /** {@inheritDoc} */ @Override public void warning(String msg, @Nullable Throwable e) { impl.warn(msg, e); - - if (consoleLog != null) - consoleLog.warn(msg, e); } /** {@inheritDoc} */ @Override public void error(String msg) { impl.error(msg); - - if (consoleLog != null) - consoleLog.error(msg); } /** {@inheritDoc} */ @Override public void error(String msg, @Nullable Throwable e) { impl.error(msg, e); - - if (consoleLog != null) - consoleLog.error(msg, e); } /** {@inheritDoc} */ @Override public boolean isTraceEnabled() { - return impl.isTraceEnabled() || (consoleLog != null && consoleLog.isTraceEnabled()); + return impl.isTraceEnabled(); } /** {@inheritDoc} */ @Override public boolean isDebugEnabled() { - return impl.isDebugEnabled() || (consoleLog != null && consoleLog.isDebugEnabled()); + return impl.isDebugEnabled(); } /** {@inheritDoc} */ @Override public boolean isInfoEnabled() { - return impl.isInfoEnabled() || (consoleLog != null && consoleLog.isInfoEnabled()); + return impl.isInfoEnabled(); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java deleted file mode 100644 index b56be27..0000000 --- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2CorrectFileNameTest.java +++ /dev/null @@ -1,94 +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.ignite.logger.log4j2; - -import java.io.File; -import junit.framework.TestCase; -import org.apache.ignite.Ignite; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.internal.util.typedef.G; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.testframework.GridTestUtils; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Tests that several grids log to files with correct names. - */ -@GridCommonTest(group = "Logger") -public class GridLog4j2CorrectFileNameTest extends TestCase { - - /** - * Tests correct behaviour in case 2 local nodes are started. - * - * @throws Exception If error occurs. - */ - public void testLogFilesTwoNodes() throws Exception { - checkOneNode(0); - checkOneNode(1); - } - - /** - * Starts the local node and checks for presence of log file. Also checks - * that this is really a log of a started node. - * - * @param id Test-local node ID. - * @throws Exception If error occurred. - */ - private void checkOneNode(int id) throws Exception { - try (Ignite ignite = G.start(getConfiguration("grid" + id))) { - String id8 = U.id8(ignite.cluster().localNode().id()); - String logPath = "work/log/ignite-" + id8 + ".log"; - File logFile = U.resolveIgnitePath(logPath); - assertNotNull("Failed to resolve path: " + logPath, logFile); - assertTrue("Log file does not exist: " + logFile, logFile.exists()); - // We have a row in log with the following content - // con >>> Local node [ID=NodeId ] - String logContent = U.readFileToString(logFile.getAbsolutePath(), - "UTF-8"); - assertTrue( - "Log file does not contain it's node ID: " + logFile, - logContent.contains(">>> Local node [ID=" - + id8.toUpperCase())); - } - } - - /** - * Creates grid configuration. - * - * @param igniteInstanceName Ignite instance name. - * @return Grid configuration. - * @throws Exception If error occurred. - */ - private static IgniteConfiguration getConfiguration(String igniteInstanceName) - throws Exception { - IgniteConfiguration cfg = new IgniteConfiguration(); - - - cfg.setIgniteInstanceName(igniteInstanceName); - // We need of a configuration file passed in - File xml = GridTestUtils - .resolveIgnitePath("modules/core/src/test/config/log4j2-test.xml"); - - assert xml != null; - assert xml.exists() == true; - - cfg.setGridLogger(new Log4J2Logger(xml)); - cfg.setConnectorConfiguration(null); - - return cfg; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitializedTest.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitializedTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitializedTest.java deleted file mode 100644 index 4758e0a..0000000 --- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2InitializedTest.java +++ /dev/null @@ -1,77 +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.ignite.logger.log4j2; - -import java.net.URL; -import java.util.UUID; -import junit.framework.TestCase; -import org.apache.ignite.IgniteCheckedException; -import org.apache.ignite.IgniteLogger; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Log4j initialized test. - */ -@GridCommonTest(group = "Logger") -public class GridLog4j2InitializedTest extends TestCase { - - /** - * @throws Exception If failed. - */ - @Override protected void setUp() throws Exception { - - } - - /** */ - public void testLogInitialize() { - - IgniteConfiguration cfg = new IgniteConfiguration(); - - cfg.setIgniteInstanceName("grid" + 1); - cfg.setNodeId(new UUID(1, 1)); - // cfg.setIgniteHome("/home/glutters/Documenti/apache-ignite/ignite-master/ignite/"); - - URL xml = U.resolveIgniteUrl("config/ignite-log4j2.xml"); - IgniteLogger log; - try { - - log = new Log4J2Logger(xml); - // log.isQuiet(); - cfg.setGridLogger(log); - } catch (IgniteCheckedException e) { - e.printStackTrace(); - return; - } - - assert log.isInfoEnabled() == true; - - if (log.isDebugEnabled()) - log.debug("This is 'debug' message."); - - log.info("This is 'info' message."); - log.warning("This is 'warning' message."); - log.warning("This is 'warning' message.", new Exception( - "It's a test warning exception")); - log.error("This is 'error' message."); - - assert log.getLogger(GridLog4j2InitializedTest.class.getName()) instanceof Log4J2Logger; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2LoggingFileTest.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2LoggingFileTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2LoggingFileTest.java deleted file mode 100644 index 5c19de0..0000000 --- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/GridLog4j2LoggingFileTest.java +++ /dev/null @@ -1,68 +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.ignite.logger.log4j2; - -import java.io.File; -import java.util.UUID; -import junit.framework.TestCase; -import org.apache.ignite.IgniteLogger; -import org.apache.ignite.logger.LoggerNodeIdAware; -import org.apache.ignite.testframework.GridTestUtils; -import org.junit.Test; - -/** - * Grid Log4j SPI test. - */ -public class GridLog4j2LoggingFileTest extends TestCase { - /** */ - private IgniteLogger log; - - /** {@inheritDoc} */ - @Override protected void setUp() throws Exception { - - - File xml = GridTestUtils - .resolveIgnitePath("modules/core/src/test/config/log4j2-test.xml"); - - assert xml != null; - assert xml.exists() == true; - - log = new Log4J2Logger(xml).getLogger(getClass()); - ((LoggerNodeIdAware) log).setNodeId(UUID.randomUUID()); - - } - - /** - * Tests log4j logging SPI. - */ - @Test - public void testLog() { - assert log.isDebugEnabled() == true; - assert log.isInfoEnabled() == true; - - log.debug("This is 'debug' message."); - log.info("This is 'info' message."); - log.warning("This is 'warning' message."); - log.warning("This is 'warning' message.", new Exception( - "It's a test warning exception")); - log.error("This is 'error' message."); - log.error("This is 'error' message.", new Exception( - "It's a test error exception")); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerSelfTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerSelfTest.java index 18fa265..a5564da 100644 --- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerSelfTest.java +++ b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerSelfTest.java @@ -44,6 +44,13 @@ public class Log4j2LoggerSelfTest extends TestCase { /** * @throws Exception If failed. */ + @Override protected void setUp() throws Exception { + Log4J2Logger.cleanup(); + } + + /** + * @throws Exception If failed. + */ public void testFileConstructor() throws Exception { File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST); http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerVerboseModeSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerVerboseModeSelfTest.java b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerVerboseModeSelfTest.java index 95c7ea8..c28108c 100644 --- a/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerVerboseModeSelfTest.java +++ b/modules/log4j2/src/test/java/org/apache/ignite/logger/log4j2/Log4j2LoggerVerboseModeSelfTest.java @@ -18,6 +18,7 @@ package org.apache.ignite.logger.log4j2; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.PrintStream; import java.util.Collections; import junit.framework.TestCase; @@ -26,6 +27,7 @@ import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.internal.util.typedef.G; import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.GridTestUtils; import org.apache.logging.log4j.Level; /** @@ -33,10 +35,18 @@ import org.apache.logging.log4j.Level; */ public class Log4j2LoggerVerboseModeSelfTest extends TestCase { /** */ - public static final String LOG_PATH_VERBOSE_TEST = "modules/core/src/test/config/log4j2-verbose-test.xml"; + private static final String LOG_PATH_VERBOSE_TEST = "modules/core/src/test/config/log4j2-verbose-test.xml"; /** - * Test does not work after another tests. Can be run from IDE as separate test. + * @throws Exception If failed. + */ + @Override protected void setUp() throws Exception { + Log4J2Logger.cleanup(); + + } + + /** + * Test works fine after other tests. Please do not forget to call Log4J2Logger.cleanup() * * @throws Exception If failed. */ @@ -47,49 +57,58 @@ public class Log4j2LoggerVerboseModeSelfTest extends TestCase { final ByteArrayOutputStream testOut = new ByteArrayOutputStream(); final ByteArrayOutputStream testErr = new ByteArrayOutputStream(); + String consoleOut = "Empty"; + String consoleErr = "Empty"; + String testMsg = "******* Hello Tester! ******* "; + try { System.setOut(new PrintStream(testOut)); System.setErr(new PrintStream(testErr)); + System.setProperty("IGNITE_QUIET", "false"); + try (Ignite ignite = G.start(getConfiguration("verboseLogGrid", LOG_PATH_VERBOSE_TEST))) { - String testMsg = "******* Hello Tester! ******* "; ignite.log().error(testMsg + Level.ERROR); ignite.log().warning(testMsg + Level.WARN); ignite.log().info(testMsg + Level.INFO); ignite.log().debug(testMsg + Level.DEBUG); ignite.log().trace(testMsg + Level.TRACE); - - String consoleOut = testOut.toString(); - String consoleErr = testErr.toString(); - - assertTrue(consoleOut.contains(testMsg + Level.INFO)); - assertTrue(consoleOut.contains(testMsg + Level.DEBUG)); - assertTrue(consoleOut.contains(testMsg + Level.TRACE)); - assertTrue(consoleOut.contains(testMsg + Level.ERROR)); - assertTrue(consoleOut.contains(testMsg + Level.WARN)); - - assertTrue(consoleErr.contains(testMsg + Level.ERROR)); - assertTrue(consoleErr.contains(testMsg + Level.WARN)); - assertTrue(!consoleErr.contains(testMsg + Level.INFO)); - assertTrue(consoleErr.contains(testMsg + Level.DEBUG)); - assertTrue(consoleErr.contains(testMsg + Level.TRACE)); } + } finally { System.setProperty("IGNITE_QUIET", "true"); System.setOut(backupSysOut); System.setErr(backupSysErr); + } - System.out.println("**************** Out Console content ***************"); - System.out.println(testOut.toString()); + testOut.flush(); + testErr.flush(); - System.err.println("**************** Err Console content ***************"); - System.err.println(testErr.toString()); - } + consoleOut = testOut.toString(); + consoleErr = testErr.toString(); + + System.out.println("**************** Out Console content ***************"); + System.out.println(consoleOut); + + System.out.println("**************** Err Console content ***************"); + System.out.println(consoleErr); + + assertTrue(consoleOut.contains(testMsg + Level.INFO)); + assertTrue(consoleOut.contains(testMsg + Level.DEBUG)); + assertTrue(consoleOut.contains(testMsg + Level.TRACE)); + assertTrue(consoleOut.contains(testMsg + Level.ERROR)); + assertTrue(consoleOut.contains(testMsg + Level.WARN)); + + assertTrue(consoleErr.contains(testMsg + Level.ERROR)); + assertTrue(consoleErr.contains(testMsg + Level.WARN)); + assertTrue(!consoleErr.contains(testMsg + Level.INFO)); + assertTrue(consoleErr.contains(testMsg + Level.DEBUG)); + assertTrue(consoleErr.contains(testMsg + Level.TRACE)); } /** @@ -108,9 +127,13 @@ public class Log4j2LoggerVerboseModeSelfTest extends TestCase { setAddresses(Collections.singleton("127.0.0.1:47500..47509")); }}); + File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_VERBOSE_TEST); + + Log4J2Logger logger = new Log4J2Logger(xml); + return new IgniteConfiguration() .setIgniteInstanceName(igniteInstanceName) - .setGridLogger(new Log4J2Logger(logPath)) + .setGridLogger(logger) .setConnectorConfiguration(null) .setDiscoverySpi(disco); } http://git-wip-us.apache.org/repos/asf/ignite/blob/f8824c86/modules/log4j2/src/test/java/org/apache/ignite/testsuites/IgniteLog4j2TestSuite.java ---------------------------------------------------------------------- diff --git a/modules/log4j2/src/test/java/org/apache/ignite/testsuites/IgniteLog4j2TestSuite.java b/modules/log4j2/src/test/java/org/apache/ignite/testsuites/IgniteLog4j2TestSuite.java index dd2b4b5..a23cb47 100644 --- a/modules/log4j2/src/test/java/org/apache/ignite/testsuites/IgniteLog4j2TestSuite.java +++ b/modules/log4j2/src/test/java/org/apache/ignite/testsuites/IgniteLog4j2TestSuite.java @@ -19,6 +19,7 @@ package org.apache.ignite.testsuites; import junit.framework.TestSuite; import org.apache.ignite.logger.log4j2.Log4j2LoggerSelfTest; +import org.apache.ignite.logger.log4j2.Log4j2LoggerVerboseModeSelfTest; /** * Log4j2 logging tests. @@ -32,6 +33,7 @@ public class IgniteLog4j2TestSuite extends TestSuite { TestSuite suite = new TestSuite("Log4j2 Logging Test Suite"); suite.addTest(new TestSuite(Log4j2LoggerSelfTest.class)); + suite.addTest(new TestSuite(Log4j2LoggerVerboseModeSelfTest.class)); return suite; }