-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
369 additions
and
2 deletions.
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.