-
Notifications
You must be signed in to change notification settings - Fork 155
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
Add new java platform logger #1361
Open
Sineaggi
wants to merge
2
commits into
neo4j:5.0
Choose a base branch
from
Sineaggi:add-java-platform-logger
base: 5.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
driver/src/main/java/org/neo4j/driver/internal/logging/JavaPlatformLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed 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.neo4j.driver.internal.logging; | ||
|
||
import org.neo4j.driver.Logger; | ||
|
||
import java.lang.System.Logger.Level; | ||
import java.util.Objects; | ||
|
||
public class JavaPlatformLogger implements Logger { | ||
private final java.lang.System.Logger delegate; | ||
|
||
public JavaPlatformLogger(System.Logger delegate) { | ||
this.delegate = Objects.requireNonNull(delegate); | ||
} | ||
|
||
@Override | ||
public void error(String message, Throwable cause) { | ||
if (delegate.isLoggable(Level.ERROR)) { | ||
delegate.log(Level.ERROR, message, cause); | ||
} | ||
} | ||
|
||
@Override | ||
public void info(String format, Object... params) { | ||
if (delegate.isLoggable(Level.INFO)) { | ||
delegate.log(Level.INFO, String.format(format, params)); | ||
} | ||
} | ||
|
||
@Override | ||
public void warn(String format, Object... params) { | ||
if (delegate.isLoggable(Level.WARNING)) { | ||
delegate.log(Level.WARNING, String.format(format, params)); | ||
} | ||
} | ||
|
||
@Override | ||
public void warn(String message, Throwable cause) { | ||
if (delegate.isLoggable(Level.WARNING)) { | ||
delegate.log(Level.WARNING, message, cause); | ||
} | ||
} | ||
|
||
@Override | ||
public void debug(String format, Object... params) { | ||
if (isDebugEnabled()) { | ||
delegate.log(Level.DEBUG, String.format(format, params)); | ||
} | ||
} | ||
|
||
@Override | ||
public void debug(String message, Throwable throwable) { | ||
if (isDebugEnabled()) { | ||
delegate.log(Level.DEBUG, message, throwable); | ||
} | ||
} | ||
|
||
@Override | ||
public void trace(String format, Object... params) { | ||
if (isTraceEnabled()) { | ||
delegate.log(Level.TRACE, String.format(format, params)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isTraceEnabled() { | ||
return delegate.isLoggable(Level.TRACE); | ||
} | ||
|
||
@Override | ||
public boolean isDebugEnabled() { | ||
return delegate.isLoggable(Level.DEBUG); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
driver/src/main/java/org/neo4j/driver/internal/logging/JavaPlatformLogging.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed 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.neo4j.driver.internal.logging; | ||
|
||
import org.neo4j.driver.Logger; | ||
import org.neo4j.driver.Logging; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Internal implementation of the java platform logging module. | ||
* <b>This class should not be used directly.</b> Please use {@link Logging#javaPlatformLogging()} factory method instead. | ||
* | ||
* @see Logging#javaPlatformLogging() | ||
*/ | ||
public class JavaPlatformLogging implements Logging, Serializable { | ||
private static final long serialVersionUID = -1145576859241657833L; | ||
|
||
public JavaPlatformLogging() { | ||
} | ||
|
||
@Override | ||
public Logger getLog(String name) { | ||
return new JavaPlatformLogger(System.getLogger(name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
driver/src/test/java/org/neo4j/driver/internal/logging/JULoggingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed 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.neo4j.driver.internal.logging; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.neo4j.driver.Logger; | ||
|
||
import java.util.logging.Level; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.junit.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
class JULoggingTest { | ||
@Test | ||
void shouldCreateLoggers() { | ||
JULogging logging = new JULogging(Level.ALL); | ||
|
||
Logger logger = logging.getLog("My Log"); | ||
|
||
assertThat(logger, instanceOf(JULogger.class)); | ||
} | ||
|
||
@Test | ||
void shouldCheckIfAvailable() { | ||
assertNull(JULogging.checkAvailability()); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
driver/src/test/java/org/neo4j/driver/internal/logging/JavaPlatformLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed 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.neo4j.driver.internal.logging; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.System.Logger; | ||
import java.lang.System.Logger.Level; | ||
|
||
class JavaPlatformLoggerTest { | ||
private final Logger logger = Mockito.mock(Logger.class); | ||
private final JavaPlatformLogger javaPlatformLogger = new JavaPlatformLogger(logger); | ||
|
||
@Test | ||
void shouldLogErrorWithMessageAndThrowable() { | ||
Mockito.when(logger.isLoggable(Level.ERROR)).thenReturn(true); | ||
String message = "Hello"; | ||
IllegalArgumentException error = new IllegalArgumentException("World"); | ||
|
||
javaPlatformLogger.error(message, error); | ||
|
||
Mockito.verify(logger).log(Level.ERROR, message, error); | ||
} | ||
|
||
@Test | ||
void shouldLogInfoWithMessageAndParams() { | ||
Mockito.when(logger.isLoggable(Level.INFO)).thenReturn(true); | ||
String message = "One %s, two %s, three %s"; | ||
Object[] params = {"111", "222", "333"}; | ||
|
||
javaPlatformLogger.info(message, params); | ||
|
||
Mockito.verify(logger).log(Level.INFO, "One 111, two 222, three 333"); | ||
} | ||
|
||
@Test | ||
void shouldLogWarnWithMessageAndParams() { | ||
Mockito.when(logger.isLoggable(Level.WARNING)).thenReturn(true); | ||
String message = "C for %s, d for %s"; | ||
Object[] params = {"cat", "dog"}; | ||
|
||
javaPlatformLogger.warn(message, params); | ||
|
||
Mockito.verify(logger).log(Level.WARNING, "C for cat, d for dog"); | ||
} | ||
|
||
@Test | ||
void shouldLogWarnWithMessageAndThrowable() { | ||
Mockito.when(logger.isLoggable(Level.WARNING)).thenReturn(true); | ||
String message = "Hello"; | ||
RuntimeException error = new RuntimeException("World"); | ||
|
||
javaPlatformLogger.warn(message, error); | ||
|
||
Mockito.verify(logger).log(Level.WARNING, message, error); | ||
} | ||
|
||
@Test | ||
void shouldLogDebugWithMessageAndParams() { | ||
Mockito.when(logger.isLoggable(Level.DEBUG)).thenReturn(true); | ||
String message = "Hello%s%s!"; | ||
Object[] params = {" ", "World"}; | ||
|
||
javaPlatformLogger.debug(message, params); | ||
|
||
Mockito.verify(logger).log(Level.DEBUG, "Hello World!"); | ||
} | ||
|
||
@Test | ||
void shouldLogTraceWithMessageAndParams() { | ||
Mockito.when(logger.isLoggable(Level.TRACE)).thenReturn(true); | ||
String message = "I'll be %s!"; | ||
Object[] params = {"back"}; | ||
|
||
javaPlatformLogger.trace(message, params); | ||
|
||
Mockito.verify(logger).log(Level.TRACE, "I'll be back!"); | ||
} | ||
|
||
@Test | ||
void shouldCheckIfDebugIsEnabled() { | ||
Mockito.when(logger.isLoggable(Level.DEBUG)).thenReturn(false); | ||
assertFalse(javaPlatformLogger.isDebugEnabled()); | ||
|
||
Mockito.when(logger.isLoggable(Level.DEBUG)).thenReturn(true); | ||
assertTrue(javaPlatformLogger.isDebugEnabled()); | ||
} | ||
|
||
@Test | ||
void shouldCheckIfTraceIsEnabled() { | ||
Mockito.when(logger.isLoggable(Level.TRACE)).thenReturn(false); | ||
assertFalse(javaPlatformLogger.isTraceEnabled()); | ||
|
||
Mockito.when(logger.isLoggable(Level.TRACE)).thenReturn(true); | ||
assertTrue(javaPlatformLogger.isTraceEnabled()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 change triggers compilation failure, caused by the following warning:
class java.util.logging.Level in module java.logging is not indirectly exported using requires transitive
.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.
Wow interesting. I tried creating a reproducer locally but wasn't able to.
According to what I've seen online we can make it
static
andtransitive
, but I'm not 100% sure. In this case it's true, the logging class is exporting JUL to downstream users (using the logger in the public return types).Reading through https://nipafx.dev/java-modules-implied-readability/ right now.
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.
Is it still failing for the same reasons?