-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: master
Are you sure you want to change the base?
IGNITE-23472 Fix JavaLogger #11615
Conversation
47549cd
to
64a31fb
Compare
modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
Show resolved
Hide resolved
IgniteLogger log2 = log1.getLogger(getClass()); | ||
|
||
assertTrue(log1.toString().contains("JavaLogger")); | ||
assertTrue(log1.toString().contains(JavaLogger.DFLT_CONFIG_PATH)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert failed and was fixed by this(true)
change in default constructor
|
||
assertTrue(logContent.contains("[INFO] Accepted info")); | ||
assertFalse(logContent.contains("[DEBUG] Ignored debug")); | ||
assertTrue(logContent.contains("[DEBUG] Accepted debug")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assert fails
log.info("Accepted info"); | ||
log.debug("Ignored debug"); | ||
|
||
log = logSupplier.get(debugCfgFile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configuration of log is still infoCfgFile because cfg field is inited only once. That's why "Accepted debug" won't be logged
c5293f7
to
0e1af24
Compare
0e1af24
to
64e382c
Compare
/** | ||
* Configure java.util.logging.config.file property | ||
*/ | ||
private void setJavaLoggerConfig() throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method can be inlined
private void setJavaLoggerConfig() throws IOException { | ||
File file = new File(U.getIgniteHome(), LOG_CONFIG_DEBUG); | ||
System.setProperty("java.util.logging.config.file", file.getPath()); | ||
LogManager.getLogManager().readConfiguration(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose user shouldn't explicitly read configuration. It is invoked inside LogManager while user tries to log first message, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LogManager reads the property when Logger.getLogger
method is called first time.
The problem is that it happens before System.setProperty
Firstly, it happens because JavaLoggerTest extends GridCommonAbstractTest which initialize IgniteUtils and IgniteUtils calls Logger.getLogger
.
Secondly, U.getIgniteHome()
also calls initialization of IgniteUtils and it calls Logger.getLogger
again.
So Logger.getLogger
is called before setProperty and that's why jul won't read this property without explicit configuration.
ignite/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
Line 319 in 67e2831
private static final Logger log = Logger.getLogger(IgniteUtils.class.getName()); |
@@ -194,7 +194,7 @@ public JavaLogger(boolean init) { | |||
* @param impl Java Logging implementation to use. | |||
*/ | |||
public JavaLogger(final Logger impl) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this constructor?
modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Check JavaLogger default constructor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sentences in comments and javadocs should end with a dot sign.
cfg.setGridLogger(log); | ||
startGrid(cfg); | ||
|
||
doSleep(2_000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't use sleep for waiting any events. Use instead GridTestUtils#waitForCondition.
@Test | ||
public void testGridLoggingWithCustomLogger() throws Exception { | ||
AtomicBoolean hasLog = new AtomicBoolean(false); | ||
Filter filter = record -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use LogListener instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListeningTestLogger did not have toString method implementation. That's why I've edited ListeningTestLogger
5f057ff
to
fc07c23
Compare
@@ -84,4 +153,5 @@ public void testLogInitialize() throws Exception { | |||
assert !log.fileName().contains("%"); | |||
assert log.fileName().contains("other-app"); | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useless change
...src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
Show resolved
Hide resolved
9e7fa81
to
c4ac02e
Compare
modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
Show resolved
Hide resolved
public void testDefaultConstructorWithProperty() throws Exception { | ||
File file = new File(U.getIgniteHome(), LOG_CONFIG_DEBUG); | ||
System.setProperty("java.util.logging.config.file", file.getPath()); | ||
// Call readConfiguration explicitly because Logger.getLogger was already called during IgniteUtils initialization |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments should be ended with a dot sign.
assertTrue(log2.toString().contains(LOG_CONFIG_DEBUG)); | ||
assertTrue(log1.isDebugEnabled()); | ||
|
||
System.clearProperty("java.util.logging.config.file"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code will not call in case of an assertion error.
cfg.setGridLogger(log); | ||
startGrid(cfg); | ||
|
||
assertTrue(GridTestUtils.waitForCondition(lsn::check, 2_000)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you wait for the check in background?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the program would log after the execution of startGrid
. However, I've checked it, and the message is actually logged before the execution of startGrid
completes.
/** {@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)), quiet, cfg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It starts invoking another constructor. Before your change there was a chain:
JavaLogger#getLogger(Object) -> new JavaLogger(Logger, configure=true)
After your change, in case root logger created as new JavaLogger(false)
the underlying logger will use constructor with no configuration and it never been configured. And the constructor JavaLogger(Logger, boolean)
is useless. Is it ok?
startGrid(cfg); | ||
|
||
assertTrue(GridTestUtils.waitForCondition(lsn::check, 2_000)); | ||
stopAllGrids(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be replaced with
try(Ignite ign = startGrid(cfg) {
...
}
@@ -271,6 +271,23 @@ public GridTestLog4jLogger(final URL cfgUrl) throws IgniteCheckedException { | |||
quiet = quiet0; | |||
} | |||
|
|||
/** | |||
* Creates new logger with given implementation. | |||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove useless blank line
File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST); | ||
|
||
assert xml != null; | ||
assert xml.exists(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the existence is already tested in GridTestUtils.resolveIgnitePath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this part of the code from Log4j2LoggerSelfTest
. Now I've fixed it in both GridTestLog4jLoggerSelfTest
and Log4j2LoggerSelfTest
.
|
||
IgniteLogger log = new GridTestLog4jLogger(xml).getLogger(getClass()); | ||
|
||
System.out.println(log.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not sout anything in tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as previous answer about Log4j2LoggerSelfTest.
162f57c
to
773eb80
Compare
773eb80
to
ee46642
Compare
2886b29
to
af6ab6f
Compare
af6ab6f
to
c3cad59
Compare
modules/core/src/main/java/org/apache/ignite/logger/java/JavaLogger.java
Show resolved
Hide resolved
modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
Show resolved
Hide resolved
IgniteLogger log2 = log1.getLogger(getClass()); | ||
assertTrue(log2.toString().contains("JavaLogger")); | ||
assertTrue(log2.toString().contains(LOG_CONFIG_DEBUG)); | ||
assertTrue(log1.isDebugEnabled()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log2?
modules/core/src/test/java/org/apache/ignite/logger/java/JavaLoggerTest.java
Show resolved
Hide resolved
|
||
IgniteConfiguration cfg = getConfiguration(getTestIgniteInstanceName()); | ||
cfg.setGridLogger(log); | ||
try (Ignite ign = startGrid(cfg)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line before this line
|
||
IgniteConfiguration cfg = getConfiguration(getTestIgniteInstanceName()); | ||
cfg.setGridLogger(log); | ||
try (Ignite ign = startGrid(cfg)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name ign -> ignore, to avoid a warning about unused variable in IDEA
...src/test/java/org/apache/ignite/testframework/junits/logger/GridTestLog4jLoggerSelfTest.java
Show resolved
Hide resolved
assertTrue(log.toString().contains("GridTestLog4jLogger")); | ||
assertTrue(log.toString().contains(xml.getPath())); | ||
|
||
log.setApplicationAndNode(null, UUID.randomUUID()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need it?
public static void beforeTests() { | ||
@Before | ||
public void beforeTest() { | ||
System.clearProperty("appId"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make GridTestLog4jLogger#APP_ID package private and use this variable
2b9847b
to
d51ef1c
Compare
d51ef1c
to
605d861
Compare
JavaLogger.configure method is invoked only once for all instances and it configures cfg field which was not static. Because of that only one instance had correct value and other instances had cfg with null value
Thank you for submitting the pull request to the Apache Ignite.
In order to streamline the review of the contribution
we ask you to ensure the following steps have been taken:
The Contribution Checklist
The description explains WHAT and WHY was made instead of HOW.
The following pattern must be used:
IGNITE-XXXX Change summary
whereXXXX
- number of JIRA issue.(see the Maintainers list)
the
green visa
attached to the JIRA ticket (see TC.Bot: Check PR)Notes
If you need any help, please email [email protected] or ask anу advice on http://asf.slack.com #ignite channel.