Skip to content

feat(logging): add support for System.Logger and deprecate Logging #1648

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

Open
wants to merge 1 commit into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package neo4j.org.testkit.backend;

import java.net.URI;
import java.util.logging.Level;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Logging;

public record Config(int port, URI uri, AuthToken authToken, Logging logging) {
public record Config(int port, URI uri, AuthToken authToken) {
static Config load() {
var env = System.getenv();
var port = Integer.parseInt(env.getOrDefault("TEST_BACKEND_PORT", "9000"));
Expand All @@ -31,12 +29,9 @@ static Config load() {
var neo4jScheme = env.getOrDefault("TEST_NEO4J_SCHEME", "neo4j");
var neo4jUser = env.getOrDefault("TEST_NEO4J_USER", "neo4j");
var neo4jPassword = env.getOrDefault("TEST_NEO4J_PASS", "password");
var level = env.get("TEST_BACKEND_LOGGING_LEVEL");
var logging = level == null || level.isEmpty() ? Logging.none() : Logging.console(Level.parse(level));
return new Config(
port,
URI.create(String.format("%s://%s:%d", neo4jScheme, neo4jHost, neo4jPort)),
AuthTokens.basic(neo4jUser, neo4jPassword),
logging);
AuthTokens.basic(neo4jUser, neo4jPassword));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@
public class Runner {
public static void main(String[] args) throws InterruptedException {
var config = Config.load();
var driver = GraphDatabase.driver(
config.uri(),
config.authToken(),
org.neo4j.driver.Config.builder().withLogging(config.logging()).build());
var driver = GraphDatabase.driver(config.uri(), config.authToken());

EventLoopGroup group = new NioEventLoopGroup();
var logging = config.logging();
var executor = Executors.newCachedThreadPool();
var workloadHandler = new WorkloadHandler(driver, executor, logging);
var readyHandler = new ReadyHandler(driver, logging);
var workloadHandler = new WorkloadHandler(driver, executor);
var readyHandler = new ReadyHandler(driver);
try {
var bootstrap = new ServerBootstrap();
bootstrap
Expand All @@ -55,7 +51,7 @@ protected void initChannel(SocketChannel channel) {
var pipeline = channel.pipeline();
pipeline.addLast("codec", new HttpServerCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(512 * 1024));
pipeline.addLast(new HttpRequestHandler(workloadHandler, readyHandler, logging));
pipeline.addLast(new HttpRequestHandler(workloadHandler, readyHandler));
}
});
var server = bootstrap.bind().sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,16 @@
import neo4j.org.testkit.backend.handler.ReadyHandler;
import neo4j.org.testkit.backend.handler.WorkloadHandler;
import neo4j.org.testkit.backend.request.WorkloadRequest;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;

public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private static final System.Logger LOGGER = System.getLogger(WorkloadHandler.class.getName());
private final ObjectMapper objectMapper = new ObjectMapper();
private final WorkloadHandler workloadHandler;
private final ReadyHandler readyHandler;
private final Logger logger;

public HttpRequestHandler(WorkloadHandler workloadHandler, ReadyHandler readyHandler, Logging logging) {
public HttpRequestHandler(WorkloadHandler workloadHandler, ReadyHandler readyHandler) {
this.workloadHandler = Objects.requireNonNull(workloadHandler);
this.readyHandler = Objects.requireNonNull(readyHandler);
this.logger = logging.getLog(getClass());
}

@Override
Expand All @@ -73,7 +70,8 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
} else if ("/ready".equals(request.uri()) && HttpMethod.GET.equals(request.method())) {
responseStage = readyHandler.ready(request.protocolVersion());
} else {
logger.warn("Unknown request %s with %s method.", request.uri(), request.method());
LOGGER.log(
System.Logger.Level.WARNING, "Unknown request %s with %s method.", request.uri(), request.method());
responseStage = CompletableFuture.completedFuture(
new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR));
}
Expand All @@ -90,7 +88,7 @@ private static void send100Continue(ChannelHandlerContext ctx) {

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.error("An unexpected error occured.", cause);
LOGGER.log(System.Logger.Level.ERROR, "An unexpected error occured.", cause);
ctx.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;

public class ReadyHandler {
private static final System.Logger LOGGER = System.getLogger(ReadyHandler.class.getName());
private final Driver driver;
private final Logger logger;

public ReadyHandler(Driver driver, Logging logging) {
public ReadyHandler(Driver driver) {
this.driver = driver;
this.logger = logging.getLog(getClass());
}

public CompletionStage<FullHttpResponse> ready(HttpVersion httpVersion) {
Expand All @@ -41,7 +38,7 @@ public CompletionStage<FullHttpResponse> ready(HttpVersion httpVersion) {
.handle((ignored, throwable) -> {
HttpResponseStatus status;
if (throwable != null) {
logger.error("An error occured during workload handling.", throwable);
LOGGER.log(System.Logger.Level.ERROR, "An error occured during workload handling.", throwable);
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
status = HttpResponseStatus.NO_CONTENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import neo4j.org.testkit.backend.request.WorkloadRequest;
import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Logger;
import org.neo4j.driver.Logging;
import org.neo4j.driver.QueryConfig;
import org.neo4j.driver.RoutingControl;
import org.neo4j.driver.Session;
Expand All @@ -43,14 +41,13 @@
import org.neo4j.driver.TransactionCallback;

public class WorkloadHandler {
private static final System.Logger LOGGER = System.getLogger(WorkloadHandler.class.getName());
private final Driver driver;
private final Executor executor;
private final Logger logger;

public WorkloadHandler(Driver driver, Executor executor, Logging logging) {
public WorkloadHandler(Driver driver, Executor executor) {
this.driver = Objects.requireNonNull(driver);
this.executor = Objects.requireNonNull(executor);
this.logger = logging.getLog(getClass());
}

public CompletionStage<FullHttpResponse> handle(HttpVersion httpVersion, WorkloadRequest workloadRequest) {
Expand All @@ -67,7 +64,7 @@ public CompletionStage<FullHttpResponse> handle(HttpVersion httpVersion, Workloa
.handle((ignored, throwable) -> {
HttpResponseStatus status;
if (throwable != null) {
logger.error("An error occured during workload handling.", throwable);
LOGGER.log(System.Logger.Level.ERROR, "An error occured during workload handling.", throwable);
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
status = HttpResponseStatus.NO_CONTENT;
Expand Down
5 changes: 5 additions & 0 deletions driver-it/LICENSES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This file contains the full license text of the included third party
libraries. For an overview of the licenses see the NOTICE.txt file.



20 changes: 20 additions & 0 deletions driver-it/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) "Neo4j"
Neo4j Sweden AB [https://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.

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.

Full license texts are found in LICENSES.txt.


Third-party licenses
--------------------

5 changes: 5 additions & 0 deletions driver-it/jul-to-slf4j-log4j-it/LICENSES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This file contains the full license text of the included third party
libraries. For an overview of the licenses see the NOTICE.txt file.



20 changes: 20 additions & 0 deletions driver-it/jul-to-slf4j-log4j-it/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) "Neo4j"
Neo4j Sweden AB [https://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.

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.

Full license texts are found in LICENSES.txt.


Third-party licenses
--------------------

73 changes: 73 additions & 0 deletions driver-it/jul-to-slf4j-log4j-it/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver-it</artifactId>
<version>6.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-java-driver-jul-to-slf4j-log4j-it</artifactId>

<packaging>jar</packaging>
<name>Neo4j Java Driver (JUL to SLF4J with Log4j IT)</name>

<scm>
<connection>scm:git:git://github.com/neo4j/neo4j-java-driver.git</connection>
<developerConnection>scm:git:[email protected]:neo4j/neo4j-java-driver.git</developerConnection>
<url>https://github.com/neo4j/neo4j-java-driver</url>
</scm>

<dependencies>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-proc:none</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* 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.it.jul.to.slf4j;

import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.layout.PatternLayout;

final class InMemoryAppender extends AbstractAppender {
private final List<LogEvent> logEvents = new ArrayList<>();

InMemoryAppender(String name, Filter filter) {
super(name, filter, PatternLayout.createDefaultLayout(), false, null);
start();
}

@Override
public void append(LogEvent event) {
logEvents.add(event);
}

List<LogEvent> getLogs() {
return logEvents;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* 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.it.jul.to.slf4j;

import static org.junit.jupiter.api.Assertions.assertFalse;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.internal.DriverFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

class LoggingIT {

private InMemoryAppender appender;

@BeforeEach
void setup() {
java.util.logging.LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
var julLogger = java.util.logging.Logger.getLogger(DriverFactory.class.getName());
julLogger.setLevel(java.util.logging.Level.INFO);

Configurator.setLevel(DriverFactory.class.getName(), Level.INFO);
var logger = (Logger) LogManager.getLogger(DriverFactory.class.getName());
appender = new InMemoryAppender("Appender", null);
logger.addAppender(appender);
logger.setAdditive(false);
}

@Test
void shouldLog() {
var driver = GraphDatabase.driver("bolt://localhost:7687");

assertFalse(appender.getLogs().isEmpty());
}
}
5 changes: 5 additions & 0 deletions driver-it/jul-to-slf4j-logback-it/LICENSES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This file contains the full license text of the included third party
libraries. For an overview of the licenses see the NOTICE.txt file.



Loading