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

Remove slf4j dependency from JDBC driver #741

Merged
merged 1 commit into from
Oct 10, 2021
Merged
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
@@ -1,6 +1,7 @@
package com.clickhouse.client.logging;

import java.util.ResourceBundle;
import java.util.function.Supplier;
import java.util.logging.Level;
import com.clickhouse.client.ClickHouseChecker;

Expand Down Expand Up @@ -29,9 +30,9 @@ public JdkLogger(java.util.logging.Logger logger) {
}

@Override
public void debug(Runnable function) {
public void debug(Supplier<?> function) {
if (function != null && logger.isLoggable(Level.FINE)) {
function.run();
log(Level.FINE, LogMessage.of(function.get()));
}
}

Expand All @@ -50,9 +51,9 @@ public void debug(Object message, Throwable t) {
}

@Override
public void error(Runnable function) {
public void error(Supplier<?> function) {
if (function != null && logger.isLoggable(Level.SEVERE)) {
function.run();
log(Level.SEVERE, LogMessage.of(function.get()));
}
}

Expand All @@ -71,9 +72,9 @@ public void error(Object message, Throwable t) {
}

@Override
public void info(Runnable function) {
public void info(Supplier<?> function) {
if (function != null && logger.isLoggable(Level.INFO)) {
function.run();
log(Level.INFO, LogMessage.of(function.get()));
}
}

Expand All @@ -92,9 +93,9 @@ public void info(Object message, Throwable t) {
}

@Override
public void trace(Runnable function) {
public void trace(Supplier<?> function) {
if (function != null && logger.isLoggable(Level.FINEST)) {
function.run();
log(Level.FINEST, LogMessage.of(function.get()));
}
}

Expand All @@ -113,9 +114,9 @@ public void trace(Object message, Throwable t) {
}

@Override
public void warn(Runnable function) {
public void warn(Supplier<?> function) {
if (function != null && logger.isLoggable(Level.WARNING)) {
function.run();
log(Level.WARNING, LogMessage.of(function.get()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.clickhouse.client.logging;

import java.util.function.Supplier;

/**
* Unified logger. Pay attention that the {@code format} follows standard
* {@link java.util.Formatter}.
Expand All @@ -11,7 +13,7 @@ public interface Logger {
*
* @param function custom function to run
*/
void debug(Runnable function);
void debug(Supplier<?> function);

/**
* Logs a message at the DEBUG level according to the specified format and
Expand All @@ -38,7 +40,7 @@ public interface Logger {
*
* @param function custom function to run
*/
void error(Runnable function);
void error(Supplier<?> function);

/**
* Logs a message at the ERROR level according to the specified format and
Expand All @@ -65,7 +67,7 @@ public interface Logger {
*
* @param function custom function to run
*/
void info(Runnable function);
void info(Supplier<?> function);

/**
* Logs a message at the INFO level according to the specified format and
Expand All @@ -92,7 +94,7 @@ public interface Logger {
*
* @param function custom function to run
*/
void trace(Runnable function);
void trace(Supplier<?> function);

/**
* Logs a message at the TRACE level according to the specified format and
Expand All @@ -119,7 +121,7 @@ public interface Logger {
*
* @param function custom function to run
*/
void warn(Runnable function);
void warn(Supplier<?> function);

/**
* Logs a message at the WARN level according to the specified format and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.clickhouse.client.logging;

import java.util.function.Supplier;

import com.clickhouse.client.ClickHouseChecker;

/**
Expand All @@ -18,9 +20,9 @@ public Slf4jLogger(org.slf4j.Logger logger) {
}

@Override
public void debug(Runnable function) {
public void debug(Supplier<?> function) {
if (function != null && logger.isDebugEnabled()) {
function.run();
logger.debug(String.valueOf(function.get()));
}
}

Expand All @@ -44,9 +46,9 @@ public void debug(Object message, Throwable t) {
}

@Override
public void error(Runnable function) {
public void error(Supplier<?> function) {
if (function != null && logger.isErrorEnabled()) {
function.run();
logger.error(String.valueOf(function.get()));
}
}

Expand All @@ -70,9 +72,9 @@ public void error(Object message, Throwable t) {
}

@Override
public void info(Runnable function) {
public void info(Supplier<?> function) {
if (function != null && logger.isInfoEnabled()) {
function.run();
logger.info(String.valueOf(function.get()));
}
}

Expand All @@ -96,9 +98,9 @@ public void info(Object message, Throwable t) {
}

@Override
public void trace(Runnable function) {
public void trace(Supplier<?> function) {
if (function != null && logger.isTraceEnabled()) {
function.run();
logger.trace(String.valueOf(function.get()));
}
}

Expand All @@ -122,9 +124,9 @@ public void trace(Object message, Throwable t) {
}

@Override
public void warn(Runnable function) {
public void warn(Supplier<?> function) {
if (function != null && logger.isWarnEnabled()) {
function.run();
logger.warn(String.valueOf(function.get()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void testLogWithFormat() {
logWithFormat("msg %s %s %s %s", 1, 2.2, "3", new Object());
}

@Test(groups = { "unit" })
public void testLogWithFunction() {
logWithFunction(() -> Collections.singleton(1));
}

@Test(groups = { "unit" })
public void testLogThrowable() {
logThrowable("msg", new Exception("test exception"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.clickhouse.client.logging;

import java.util.function.Supplier;

import org.testng.Assert;

public abstract class LoggerTest {
Expand All @@ -24,15 +26,15 @@ protected void checkInstance(Class<?> clazz) {
protected void logMessage(Object message) {
Logger logger = getLogger(LoggerTest.class);

logger.trace(null);
logger.trace((Object) null);
logger.trace(message);
logger.debug(null);
logger.debug((Object) null);
logger.debug(message);
logger.info(null);
logger.info((Object) null);
logger.info(message);
logger.warn(null);
logger.warn((Object) null);
logger.warn(message);
logger.error(null);
logger.error((Object) null);
logger.error(message);
}

Expand All @@ -54,6 +56,26 @@ protected void logWithFormat(Object message, Object... arguments) {
logger.error(message, arguments);
}

protected void logWithFunction(Supplier<?> function) {
Logger logger = getLogger(LoggerTest.class);

logger.trace((Supplier<?>) null);
logger.trace(() -> null);
logger.trace(function);
logger.debug((Supplier<?>) null);
logger.debug(() -> null);
logger.debug(function);
logger.info((Supplier<?>) null);
logger.info(() -> null);
logger.info(function);
logger.warn((Supplier<?>) null);
logger.warn(() -> null);
logger.warn(function);
logger.error((Supplier<?>) null);
logger.error(() -> null);
logger.error(function);
}

protected void logThrowable(Object message, Throwable t) {
Logger logger = getLogger(Slf4jLoggerTest.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void testLogWithFormat() {
logWithFormat("msg %s %s %s %s", 1, 2.2, "3", new Object());
}

@Test(groups = { "unit" })
public void testLogWithFunction() {
logWithFunction(() -> Collections.singleton(2L));
}

@Test(groups = { "unit" })
public void testLogThrowable() {
logThrowable("msg", new Exception("test exception"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.clickhouse.client.ClickHouseConfig;
import com.clickhouse.client.ClickHouseDataStreamFactory;
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.data.ClickHousePipedStream;
import com.clickhouse.client.exception.ClickHouseException;
import com.clickhouse.client.grpc.impl.Exception;
Expand Down Expand Up @@ -72,6 +73,8 @@ protected boolean updateStatus(Result result) {
log.info("%s.%s [ %s ] {%s} <%s> %s: %s", e.getTime(), e.getTimeMicroseconds(), e.getThreadId(),
e.getQueryId(), logLevel, e.getSource(), e.getText());
}

return ClickHouseUtils.format("Logged %d entries from server", result.getLogsList().size());
});

boolean proceed = true;
Expand Down
8 changes: 0 additions & 8 deletions clickhouse-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
Expand Down Expand Up @@ -218,10 +214,6 @@
<pattern>net.jpountz</pattern>
<shadedPattern>${shade.base}.jpountz</shadedPattern>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>${shade.base}.slf4j</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package ru.yandex.clickhouse;

import org.slf4j.LoggerFactory;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import javax.sql.DataSource;

import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.logging.Logger;
import com.clickhouse.client.logging.LoggerFactory;

import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -27,7 +27,7 @@
* which test hosts for availability. By default, this option is turned off.
*/
public class BalancedClickhouseDataSource implements DataSource {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(BalancedClickhouseDataSource.class);
private static final Logger log = LoggerFactory.getLogger(BalancedClickhouseDataSource.class);
private static final Pattern URL_TEMPLATE = Pattern.compile(JDBC_CLICKHOUSE_PREFIX + "" +
"//([a-zA-Z0-9_\\[\\]:,.-]+)" +
"(/[a-zA-Z0-9_]+" +
Expand Down Expand Up @@ -108,7 +108,7 @@ private BalancedClickhouseDataSource(final List<String> urls, ClickHouseProperti
if (driver.acceptsURL(url)) {
allUrls.add(url);
} else {
log.error("that url is has not correct format: {}", url);
log.error("that url is has not correct format: %s", url);
}
} catch (SQLException e) {
throw new IllegalArgumentException("error while checking url: " + url, e);
Expand Down Expand Up @@ -146,7 +146,7 @@ private boolean ping(final String url) {
driver.connect(url, properties).createStatement().execute("SELECT 1");
return true;
} catch (Exception e) {
log.debug("Unable to connect using {}", url, e);
log.debug("Unable to connect using %s", url, e);
return false;
}
}
Expand All @@ -160,12 +160,12 @@ public synchronized int actualize() {
List<String> enabledUrls = new ArrayList<String>(allUrls.size());

for (String url : allUrls) {
log.debug("Pinging disabled url: {}", url);
log.debug("Pinging disabled url: %s", url);
if (ping(url)) {
log.debug("Url is alive now: {}", url);
log.debug("Url is alive now: %s", url);
enabledUrls.add(url);
} else {
log.debug("Url is dead now: {}", url);
log.debug("Url is dead now: %s", url);
}
}

Expand Down Expand Up @@ -260,7 +260,7 @@ public int getLoginTimeout() throws SQLException {
/**
* {@inheritDoc}
*/
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}

Expand Down
Loading