This is an automated email from the ASF dual-hosted git repository.
timoninmaxim pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 5a9dab62bf3 IGNITE-23472 Fix JavaLogger doesn't log configuration file
path (#11615)
5a9dab62bf3 is described below
commit 5a9dab62bf34bd4fc666b89cacadf6241999bcc7
Author: Alexander Chesnokov <[email protected]>
AuthorDate: Tue Dec 3 11:15:04 2024 +0300
IGNITE-23472 Fix JavaLogger doesn't log configuration file path (#11615)
---
.../org/apache/ignite/logger/java/JavaLogger.java | 27 +++--
modules/core/src/test/config/jul-debug.properties | 50 +++++++++
.../apache/ignite/logger/java/JavaLoggerTest.java | 112 +++++++++++++++++++--
.../ignite/testframework/ListeningTestLogger.java | 8 ++
.../junits/logger/GridTestLog4jLogger.java | 20 +++-
.../junits/logger/GridTestLog4jLoggerSelfTest.java | 95 ++++++++++++++---
6 files changed, 275 insertions(+), 37 deletions(-)
diff --git
a/modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
b/modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
index 4643352f1ee..619f488fead 100644
--- a/modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
+++ b/modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
@@ -117,6 +117,7 @@ public class JavaLogger implements IgniteLoggerEx {
/** Path to configuration file. */
@GridToStringExclude
+ @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized")
private String cfg;
/** Quiet flag. */
@@ -134,7 +135,7 @@ public class JavaLogger implements IgniteLoggerEx {
* Creates new logger.
*/
public JavaLogger() {
- this(!isConfigured());
+ this(true);
}
/**
@@ -188,15 +189,6 @@ public class JavaLogger implements IgniteLoggerEx {
quiet = true;
}
- /**
- * Creates new logger with given implementation.
- *
- * @param impl Java Logging implementation to use.
- */
- public JavaLogger(final Logger impl) {
- this(impl, true);
- }
-
/**
* Creates new logger with given implementation.
*
@@ -214,13 +206,26 @@ public class JavaLogger implements IgniteLoggerEx {
quiet = quiet0;
}
+ /**
+ * Creates new logger with given parameters.
+ *
+ * @param impl Java Logging implementation to use.
+ * @param cfg Path to configuration.
+ */
+ private JavaLogger(Logger impl, String cfg) {
+ this(impl, true);
+
+ if (cfg != null)
+ this.cfg = cfg;
+ }
+
/** {@inheritDoc} */
@Override public IgniteLogger getLogger(Object ctgr) {
return new JavaLogger(ctgr == null
? Logger.getLogger("")
: Logger.getLogger(ctgr instanceof Class
? ((Class<?>)ctgr).getName()
- : String.valueOf(ctgr)));
+ : String.valueOf(ctgr)), cfg);
}
/**
diff --git a/modules/core/src/test/config/jul-debug.properties
b/modules/core/src/test/config/jul-debug.properties
new file mode 100644
index 00000000000..2e3165de73b
--- /dev/null
+++ b/modules/core/src/test/config/jul-debug.properties
@@ -0,0 +1,50 @@
+#
+# 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.
+#
+
+# Comma-separated list of logging "handlers". Note that some of them may be
+# reconfigured (or even removed) at runtime according to system properties.
+#
+# By default all messages will be passed to console and file.
+#
+handlers=java.util.logging.ConsoleHandler,
org.apache.ignite.logger.java.JavaLoggerFileHandler
+
+#
+# Default global logging level.
+# This specifies which kinds of events are logged across all loggers.
+# For any given category this global level can be overriden by a category
+# specific level.
+# Note that handlers also have a separate level setting to limit messages
+# printed through it.
+#
+.level=FINE
+
+#
+# Console handler logs all messages with importance level `INFO` and above
+# into standard error stream (`System.err`).
+#
+java.util.logging.ConsoleHandler.formatter=org.apache.ignite.logger.java.JavaLoggerFormatter
+java.util.logging.ConsoleHandler.level=INFO
+
+#
+# File handler logs all messages into files with pattern `ignite-%{id8}.%g.log`
+# under `$IGNITE_HOME/work/log/` directory. The placeholder `%{id8}` is a
truncated node ID.
+#
+org.apache.ignite.logger.java.JavaLoggerFileHandler.formatter=org.apache.ignite.logger.java.JavaLoggerFormatter
+org.apache.ignite.logger.java.JavaLoggerFileHandler.pattern=%{app}%{id8}.%g.log
+org.apache.ignite.logger.java.JavaLoggerFileHandler.level=INFO
+org.apache.ignite.logger.java.JavaLoggerFileHandler.limit=10485760
+org.apache.ignite.logger.java.JavaLoggerFileHandler.count=10
diff --git
a/modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
b/modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
index 1971ab83458..ef03e195e97 100644
---
a/modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
@@ -17,39 +17,133 @@
package org.apache.ignite.logger.java;
+import java.io.File;
import java.util.UUID;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.logger.IgniteLoggerEx;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.ListeningTestLogger;
+import org.apache.ignite.testframework.LogListener;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.apache.ignite.testframework.junits.common.GridCommonTest;
import org.junit.Test;
-import static org.junit.Assert.assertTrue;
+import static org.apache.ignite.logger.java.JavaLogger.DFLT_CONFIG_PATH;
/**
* Java logger test.
*/
@GridCommonTest(group = "Logger")
-public class JavaLoggerTest {
- /** */
- @SuppressWarnings({"FieldCanBeLocal"})
- private IgniteLogger log;
+public class JavaLoggerTest extends GridCommonAbstractTest {
+ /**
+ * Path to jul configuration with DEBUG enabled.
+ */
+ private static final String LOG_CONFIG_DEBUG =
"modules/core/src/test/config/jul-debug.properties";
+
+ /**
+ * Reset JavaLogger.
+ */
+ @Override protected void afterTest() throws Exception {
+ GridTestUtils.setFieldValue(JavaLogger.class, JavaLogger.class,
"inited", false);
+ }
+
+ /**
+ * Check JavaLogger default constructor.
+ */
+ @Test
+ public void testDefaultConstructorWithDefaultConfig() {
+ IgniteLogger log1 = new JavaLogger();
+ IgniteLogger log2 = log1.getLogger(getClass());
+
+ assertTrue(log1.toString().contains("JavaLogger"));
+ assertTrue(log1.toString().contains(DFLT_CONFIG_PATH));
+
+ assertTrue(log2.toString().contains("JavaLogger"));
+ assertTrue(log2.toString().contains(DFLT_CONFIG_PATH));
+ }
+
+ /**
+ * Check non-configured constructor of JavaLogger.
+ */
+ @Test
+ public void testNotInitializedLogger() {
+ IgniteLogger log1 = new JavaLogger(Logger.getAnonymousLogger(), false);
+ assertTrue(log1.toString().contains("JavaLogger"));
+ assertTrue(log1.toString().contains("null"));
+
+ IgniteLogger log2 = log1.getLogger(getClass());
+ assertTrue(log2.toString().contains("JavaLogger"));
+ assertTrue(log2.toString().contains(DFLT_CONFIG_PATH));
+ }
+
+ /**
+ * Check JavaLogger constructor from java.util.logging.config.file
property.
+ */
+ @Test
+ public void testDefaultConstructorWithProperty() throws Exception {
+ String cfgPathProp = "java.util.logging.config.file";
+ String oldPropVal = System.getProperty(cfgPathProp);
+
+ try {
+ File file = new File(U.getIgniteHome(), LOG_CONFIG_DEBUG);
+ System.setProperty(cfgPathProp, file.getPath());
+ // Call readConfiguration explicitly because Logger.getLogger was
already called during IgniteUtils initialization.
+ LogManager.getLogManager().readConfiguration();
+
+ IgniteLogger log1 = new JavaLogger();
+ assertTrue(log1.toString().contains("JavaLogger"));
+ assertTrue(log1.toString().contains(LOG_CONFIG_DEBUG));
+ assertTrue(log1.isDebugEnabled());
+
+ IgniteLogger log2 = log1.getLogger(getClass());
+ assertTrue(log2.toString().contains("JavaLogger"));
+ assertTrue(log2.toString().contains(LOG_CONFIG_DEBUG));
+ assertTrue(log2.isDebugEnabled());
+ }
+ finally {
+ if (oldPropVal == null)
+ System.clearProperty(cfgPathProp);
+ else
+ System.setProperty(cfgPathProp, oldPropVal);
+ }
+ }
+
+ /**
+ * Check Grid logging.
+ */
+ @Test
+ public void testGridLoggingWithDefaultLogger() throws Exception {
+ LogListener lsn = LogListener.matches("JavaLogger [quiet=true,")
+ .andMatches(DFLT_CONFIG_PATH)
+ .build();
+
+ ListeningTestLogger log = new ListeningTestLogger(new JavaLogger(),
lsn);
+
+ IgniteConfiguration cfg =
getConfiguration(getTestIgniteInstanceName());
+ cfg.setGridLogger(log);
+
+ try (Ignite ignore = startGrid(cfg)) {
+ assertTrue(lsn.check());
+ }
+ }
/**
* @throws Exception If failed.
*/
@Test
public void testLogInitialize() throws Exception {
- log = new JavaLogger();
+ JavaLogger log = new JavaLogger();
((JavaLogger)log).setWorkDirectory(U.defaultWorkDirectory());
((IgniteLoggerEx)log).setApplicationAndNode(null,
UUID.fromString("00000000-1111-2222-3333-444444444444"));
- System.out.println(log.toString());
-
assertTrue(log.toString().contains("JavaLogger"));
- assertTrue(log.toString().contains(JavaLogger.DFLT_CONFIG_PATH));
+ assertTrue(log.toString().contains(DFLT_CONFIG_PATH));
if (log.isDebugEnabled())
log.debug("This is 'debug' message.");
diff --git
a/modules/core/src/test/java/org/apache/ignite/testframework/ListeningTestLogger.java
b/modules/core/src/test/java/org/apache/ignite/testframework/ListeningTestLogger.java
index 0c2b69fcb64..f58d427bb35 100644
---
a/modules/core/src/test/java/org/apache/ignite/testframework/ListeningTestLogger.java
+++
b/modules/core/src/test/java/org/apache/ignite/testframework/ListeningTestLogger.java
@@ -18,6 +18,7 @@
package org.apache.ignite.testframework;
import java.util.Collection;
+import java.util.Objects;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer;
import org.apache.ignite.IgniteLogger;
@@ -209,6 +210,13 @@ public class ListeningTestLogger implements IgniteLogger {
return null;
}
+ /**
+ * @return String representation of original logger.
+ */
+ @Override public String toString() {
+ return Objects.toString(echo);
+ }
+
/**
* Applies listeners whose pattern is found in the message.
*
diff --git
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLogger.java
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLogger.java
index a8da1c64bfd..592711223b5 100644
---
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLogger.java
+++
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLogger.java
@@ -271,6 +271,22 @@ public class GridTestLog4jLogger implements IgniteLoggerEx
{
quiet = quiet0;
}
+ /**
+ * Creates new logger with given implementation.
+ */
+ private GridTestLog4jLogger(final Logger impl, String cfg) {
+ assert impl != null;
+
+ addConsoleAppenderIfNeeded(null, new C1<Boolean, Logger>() {
+ @Override public Logger apply(Boolean init) {
+ return impl;
+ }
+ });
+
+ quiet = quiet0;
+ this.cfg = cfg;
+ }
+
/**
* Checks if Log4j is already configured within this VM or not.
*
@@ -489,7 +505,7 @@ public class GridTestLog4jLogger implements IgniteLoggerEx {
? LogManager.getRootLogger()
: ctgr instanceof Class
? LogManager.getLogger(((Class<?>)ctgr).getName())
- : LogManager.getLogger(ctgr.toString()));
+ : LogManager.getLogger(ctgr.toString()), cfg);
}
/** {@inheritDoc} */
@@ -497,7 +513,7 @@ public class GridTestLog4jLogger implements IgniteLoggerEx {
if (!impl.isTraceEnabled())
warning("Logging at TRACE level without checking if TRACE level is
enabled: " + msg);
- assert impl.isTraceEnabled() : "Logging at TRACE level without
checking if TRACE level is enabled: " + msg;
+ assert impl.isTraceEnabled() : "Logging at TRACE level without
checking if TRACE level is enabled: " + msg;
impl.trace(msg);
}
diff --git
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
index 8f6ee15e247..3b4212baba3 100644
---
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
@@ -17,14 +17,18 @@
package org.apache.ignite.testframework.junits.logger;
+import java.io.File;
+import java.net.URL;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.internal.logger.IgniteLoggerEx;
import org.apache.ignite.internal.util.lang.RunnableX;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configurator;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -32,60 +36,107 @@ import org.junit.runners.JUnit4;
import static java.lang.String.format;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
/**
* Checks that assertion error will be thrown, if logging for the level
disabled and log message on this level was invoked.
*/
@RunWith(JUnit4.class)
public class GridTestLog4jLoggerSelfTest {
+ /** Path to test configuration. */
+ private static final String LOG_PATH_TEST =
"modules/log4j2/src/test/config/log4j2-test.xml";
+
/** Logger message. */
private static final String LOG_MESSAGE = "TEST MESSAGE";
/** Assertion message formatter. */
private static final String ASSERTION_FORMAT_MSG = "Logging at %s level
without checking if %s level is enabled: " + LOG_MESSAGE;
- /** Logger. */
- private static final GridTestLog4jLogger LOGGER = new
GridTestLog4jLogger();
-
/** Default root level. */
private static final Level defaultRootLevel =
LogManager.getRootLogger().getLevel();
/** */
- @BeforeClass
- public static void beforeTests() {
+ @Before
+ public void beforeTest() {
+ GridTestUtils.setFieldValue(GridTestLog4jLogger.class,
GridTestLog4jLogger.class, "inited", false);
Configurator.setRootLevel(Level.WARN);
}
/** */
- @AfterClass
- public static void afterTests() {
+ @After
+ public void afterTest() {
Configurator.setRootLevel(defaultRootLevel);
assertEquals(defaultRootLevel,
LoggerContext.getContext(false).getConfiguration().getRootLogger().getLevel());
}
+ /** */
+ @Test
+ public void testFileConstructor() throws Exception {
+ File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);
+
+ assert xml != null;
+
+ IgniteLoggerEx log = new
GridTestLog4jLogger(xml).getLogger(getClass());
+
+ assertTrue(log.toString().contains("GridTestLog4jLogger"));
+ assertTrue(log.toString().contains(xml.getPath()));
+
+ checkLog(log);
+ }
+
+ /** */
+ @Test
+ public void testUrlConstructor() throws Exception {
+ File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);
+
+ assert xml != null;
+
+ URL url = xml.toURI().toURL();
+ IgniteLoggerEx log = new
GridTestLog4jLogger(url).getLogger(getClass());
+
+ assertTrue(log.toString().contains("GridTestLog4jLogger"));
+ assertTrue(log.toString().contains(url.getPath()));
+
+ checkLog(log);
+ }
+
+ /** */
+ @Test
+ public void testPathConstructor() throws Exception {
+ IgniteLoggerEx log = new
GridTestLog4jLogger(LOG_PATH_TEST).getLogger(getClass());
+
+ assertTrue(log.toString().contains("GridTestLog4jLogger"));
+ assertTrue(log.toString().contains(LOG_PATH_TEST));
+
+ checkLog(log);
+ }
+
/** */
@Test
public void testDebug() {
- assertFalse(LOGGER.isDebugEnabled());
+ GridTestLog4jLogger log = new GridTestLog4jLogger();
+ assertFalse(log.isDebugEnabled());
- tryLog(() -> LOGGER.debug(LOG_MESSAGE), Level.DEBUG);
+ tryLog(() -> log.debug(LOG_MESSAGE), Level.DEBUG);
}
/** */
@Test
public void testInfo() {
- assertFalse(LOGGER.isInfoEnabled());
+ GridTestLog4jLogger log = new GridTestLog4jLogger();
+ assertFalse(log.isInfoEnabled());
- tryLog(() -> LOGGER.info(LOG_MESSAGE), Level.INFO);
+ tryLog(() -> log.info(LOG_MESSAGE), Level.INFO);
}
/** */
@Test
public void testTrace() {
- assertFalse(LOGGER.isTraceEnabled());
+ GridTestLog4jLogger log = new GridTestLog4jLogger();
+ assertFalse(log.isTraceEnabled());
- tryLog(() -> LOGGER.trace(LOG_MESSAGE), Level.TRACE);
+ tryLog(() -> log.trace(LOG_MESSAGE), Level.TRACE);
}
/** */
@@ -94,4 +145,18 @@ public class GridTestLog4jLoggerSelfTest {
GridTestUtils.assertThrows(null, clo, AssertionError.class,
assertionMsg);
}
+
+ /**
+ * Tests logging SPI.
+ */
+ private void checkLog(IgniteLogger log) {
+ assert !log.isDebugEnabled();
+ assert log.isInfoEnabled();
+
+ 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"));
+ }
}