Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-23472 Fix JavaLogger #11615

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public class JavaLogger implements IgniteLoggerEx {

/** Path to configuration file. */
@GridToStringExclude
@SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized")
private String cfg;

/** Quiet flag. */
Expand All @@ -134,7 +135,7 @@ public class JavaLogger implements IgniteLoggerEx {
* Creates new logger.
*/
public JavaLogger() {
this(!isConfigured());
this(true);
timoninmaxim marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -188,15 +189,6 @@ public JavaLogger(boolean init) {
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.
*
Expand All @@ -214,13 +206,26 @@ public JavaLogger(final Logger impl, boolean configure) {
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)
timoninmaxim marked this conversation as resolved.
Show resolved Hide resolved
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);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions modules/core/src/test/config/jul-debug.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# 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

#
# Uncomment to allow debug messages for entire Ignite package.
#
#org.apache.ignite.level=FINE

#
# Uncomment this line to enable cache query execution tracing.
#
#org.apache.ignite.cache.queries.level=FINE

#
# Uncomment to disable courtesy notices, such as SPI configuration
# consistency warnings.
#
#org.apache.ignite.CourtesyConfigNotice.level=OFF

#
# 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
Original file line number Diff line number Diff line change
Expand Up @@ -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";

timoninmaxim marked this conversation as resolved.
Show resolved Hide resolved
/**
* Reset JavaLogger.
*/
@Override protected void afterTest() throws Exception {
GridTestUtils.setFieldValue(JavaLogger.class, JavaLogger.class, "inited", false);
}

chesnokoff marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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 {
timoninmaxim marked this conversation as resolved.
Show resolved Hide resolved
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);
timoninmaxim marked this conversation as resolved.
Show resolved Hide resolved

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());
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.");
Expand All @@ -70,12 +164,11 @@ public void testLogInitialize() throws Exception {
assert !log.fileName().contains("%");
assert log.fileName().contains("ignite");

System.clearProperty("java.util.logging.config.file");
GridTestUtils.setFieldValue(JavaLogger.class, JavaLogger.class, "inited", false);

log = new JavaLogger();

((JavaLogger)log).setWorkDirectory(U.defaultWorkDirectory());
log.setWorkDirectory(U.defaultWorkDirectory());
((IgniteLoggerEx)log).setApplicationAndNode("other-app", UUID.fromString("00000000-1111-2222-3333-444444444444"));

assert log.fileName() != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -209,6 +210,13 @@ public void clearListeners() {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ public GridTestLog4jLogger(final URL cfgUrl) throws IgniteCheckedException {
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.
*
Expand Down Expand Up @@ -489,15 +505,15 @@ public static Collection<String> logFiles() {
? LogManager.getRootLogger()
: ctgr instanceof Class
? LogManager.getLogger(((Class<?>)ctgr).getName())
: LogManager.getLogger(ctgr.toString()));
: LogManager.getLogger(ctgr.toString()), cfg);
}

/** {@inheritDoc} */
@Override public void trace(String msg) {
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);
}
Expand Down
Loading