Skip to content

Commit

Permalink
Log enhancement: Use slf4j.Logger to replace System.out and print the…
Browse files Browse the repository at this point in the history
… exception stack trace.
  • Loading branch information
xiaojunxiang2023 committed Oct 17, 2024
1 parent 569d577 commit f7ae36e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/main/java/io/milvus/pool/ClientPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;

public class ClientPool<C, T> {
protected static final Logger logger = LoggerFactory.getLogger(ClientPool.class);
protected GenericKeyedObjectPool<String, T> clientPool;
protected PoolConfig config;
protected PoolClientFactory<C, T> clientFactory;
Expand Down Expand Up @@ -48,7 +51,7 @@ public T getClient(String key) {
try {
return clientPool.borrowObject(key);
} catch (Exception e) {
System.out.println("Failed to get client, exception: " + e.getMessage());
logger.error("Failed to get client, exception: ", e);
return null;
}
}
Expand All @@ -65,7 +68,7 @@ public void returnClient(String key, T grpcClient) {
try {
clientPool.returnObject(key, grpcClient);
} catch (Exception e) {
System.out.println("Failed to return client, exception: " + e.getMessage());
logger.error("Failed to return client, exception: " + e);
throw e;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/milvus/pool/PoolClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class PoolClientFactory<C, T> extends BaseKeyedPooledObjectFactory<String, T> {
protected static final Logger logger = LoggerFactory.getLogger(PoolClientFactory.class);
private final C config;
private Constructor<?> constructor;
private Method closeMethod;
Expand All @@ -22,7 +25,7 @@ public PoolClientFactory(C config, String clientClassName) throws ClassNotFoundE
closeMethod = clientCls.getMethod("close", long.class);
verifyMethod = clientCls.getMethod("clientIsReady");
} catch (Exception e) {
System.out.println("Failed to create client pool factory, exception: " + e.getMessage());
logger.error("Failed to create client pool factory, exception: ", e);
throw e;
}
}
Expand Down Expand Up @@ -54,7 +57,7 @@ public boolean validateObject(String key, PooledObject<T> p) {
T client = p.getObject();
return (boolean) verifyMethod.invoke(client);
} catch (Exception e) {
System.out.println("Failed to validate client, exception: " + e.getMessage());
logger.error("Failed to validate client, exception: " + e);
return true;
}
}
Expand Down

0 comments on commit f7ae36e

Please sign in to comment.