Skip to content

Commit 553deec

Browse files
author
chengyitian
committed
AJ-529、AJ-492: support slf4j;
1 parent 971707d commit 553deec

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@
6767
<version>3.12.0</version>
6868
<scope>test</scope>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.slf4j</groupId>
72+
<artifactId>slf4j-api</artifactId>
73+
<version>2.0.7</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.slf4j</groupId>
77+
<artifactId>slf4j-simple</artifactId>
78+
<version>2.0.7</version>
79+
</dependency>
7080
</dependencies>
7181

7282
<distributionManagement>

src/com/xxdb/DBConnection.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import com.xxdb.data.*;
1616
import com.xxdb.data.Void;
1717
import com.xxdb.io.*;
18-
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1920
import javax.net.ssl.SSLContext;
2021
import javax.net.ssl.SSLSocketFactory;
2122
import javax.net.ssl.TrustManager;
@@ -55,6 +56,7 @@ public class DBConnection {
5556
private int[] serverVersion_;
5657
private boolean isReverseStreaming_ = false;
5758

59+
private static final Logger log = LoggerFactory.getLogger(DBConnection.class);
5860

5961
private enum ServerExceptionState {
6062
NEW_LEADER, WAIT, CONN_FAIL, OTHER_EXCEPTION, DATA_NODE_NOT_AVAILABLE
@@ -200,6 +202,7 @@ private boolean connect()throws IOException{
200202
socket_.connect(new InetSocketAddress(hostName_,port_), 3000);
201203
}
202204
} catch (ConnectException ex) {
205+
log.error("Connect to " + this.hostName_ + ":" + this.port_ + " failed.");
203206
throw ex;
204207
}
205208
if (this.connTimeout_ > 0) {
@@ -251,6 +254,7 @@ private boolean connect()throws IOException{
251254
}
252255
}
253256

257+
log.info("Connect to " + this.hostName_ + ":" + this.port_ + " successfully.");
254258
return true;
255259
}
256260

@@ -838,7 +842,7 @@ public boolean connect(String hostName, int port, String userId, String password
838842
}
839843

840844
if (pMinNode != null && !pMinNode.equals(connectedNode)){
841-
System.out.println("Switch to node: " + pMinNode.hostName + ":" + pMinNode.port);
845+
log.info("Switch to node: " + pMinNode.hostName + ":" + pMinNode.port);
842846
conn_.close();
843847
switchDataNode(pMinNode);
844848
}
@@ -880,14 +884,17 @@ public void switchDataNode(Node node) throws IOException{
880884
do {
881885
if (node.hostName != null && node.hostName.length() > 0){
882886
if (connectNode(node)){
887+
log.info("Switch to node: " + node.hostName + ":" + node.port + " successfully.");
883888
break;
884889
}
885890
}
886891
if (nodes_.isEmpty()){
887-
throw new RuntimeException("Failed to connect to " + node.hostName + ":" + node.port);
892+
log.error("Connect to " + node.hostName + ":" + node.port + " failed.");
893+
throw new RuntimeException("Connect to " + node.hostName + ":" + node.port + " failed.");
888894
}
889895
int index = nodeRandom_.nextInt(nodes_.size());
890896
if (connectNode(nodes_.get(index))){
897+
log.info("Switch to node: " + nodes_.get(index).hostName + ":" + nodes_.get(index).port + " successfully.");
891898
break;
892899
}
893900
try {
@@ -903,7 +910,7 @@ public void switchDataNode(Node node) throws IOException{
903910
}
904911

905912
public boolean connectNode(Node node) throws IOException{
906-
System.out.println("Connect to " + node.hostName + ":" + node.port + ".");
913+
log.info("Connect to " + node.hostName + ":" + node.port + ".");
907914
while (!closed_){
908915
try {
909916
return conn_.connect(node.hostName, node.port, uid_, pwd_, connTimeout_);

0 commit comments

Comments
 (0)