Skip to content

Commit bac5a3f

Browse files
Merge pull request #180 from rabbitmq/rabbitmq-java-client-114
Add SLF4J
2 parents f8a7e2b + 6216826 commit bac5a3f

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

pom.xml

+17-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5454
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5555

56+
<slf4j.version>1.7.21</slf4j.version>
57+
<commons-cli.version>1.1</commons-cli.version>
58+
<junit.version>4.12</junit.version>
59+
5660
<!--
5761
These groovy scripts are used later in this POM file to generate
5862
source files and resources for the library itself and for the
@@ -459,16 +463,27 @@
459463
</profiles>
460464

461465
<dependencies>
466+
<dependency>
467+
<groupId>org.slf4j</groupId>
468+
<artifactId>slf4j-api</artifactId>
469+
<version>${slf4j.version}</version>
470+
</dependency>
462471
<dependency>
463472
<groupId>commons-cli</groupId>
464473
<artifactId>commons-cli</artifactId>
465-
<version>1.1</version>
474+
<version>${commons-cli.version}</version>
466475
<scope>test</scope>
467476
</dependency>
468477
<dependency>
469478
<groupId>junit</groupId>
470479
<artifactId>junit</artifactId>
471-
<version>4.12</version>
480+
<version>${junit.version}</version>
481+
<scope>test</scope>
482+
</dependency>
483+
<dependency>
484+
<groupId>org.slf4j</groupId>
485+
<artifactId>slf4j-simple</artifactId>
486+
<version>${slf4j.version}</version>
472487
<scope>test</scope>
473488
</dependency>
474489
</dependencies>

src/main/java/com/rabbitmq/client/impl/ForgivingExceptionHandler.java

+16-32
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@
1515

1616
package com.rabbitmq.client.impl;
1717

18+
import com.rabbitmq.client.*;
19+
import org.slf4j.LoggerFactory;
20+
1821
import java.io.IOException;
1922
import java.net.ConnectException;
20-
import java.util.concurrent.TimeoutException;
21-
22-
import com.rabbitmq.client.AMQP;
23-
import com.rabbitmq.client.AlreadyClosedException;
24-
import com.rabbitmq.client.Channel;
25-
import com.rabbitmq.client.Connection;
26-
import com.rabbitmq.client.Consumer;
27-
import com.rabbitmq.client.ExceptionHandler;
28-
import com.rabbitmq.client.TopologyRecoveryException;
2923

3024
/**
3125
* An implementation of {@link com.rabbitmq.client.ExceptionHandler} that does not
@@ -38,12 +32,7 @@
3832
*/
3933
public class ForgivingExceptionHandler implements ExceptionHandler {
4034
public void handleUnexpectedConnectionDriverException(Connection conn, Throwable exception) {
41-
// TODO: Log this somewhere, just in case we have a bug like
42-
// 16272 where exceptions aren't being propagated properly
43-
// again.
44-
45-
//System.err.println("DefaultExceptionHandler:");
46-
//exception.printStackTrace();
35+
log("An unexpected connection driver error occured", exception);
4736
}
4837

4938
public void handleReturnListenerException(Channel channel, Throwable exception) {
@@ -82,48 +71,43 @@ public void handleConnectionRecoveryException(Connection conn, Throwable excepti
8271
if (exception instanceof ConnectException) {
8372
// no-op
8473
} else {
85-
System.err.println("Caught an exception during connection recovery!");
86-
exception.printStackTrace(System.err);
74+
log("Caught an exception during connection recovery!", exception);
8775
}
8876
}
8977

9078
/**
9179
* @since 3.3.0
9280
*/
9381
public void handleChannelRecoveryException(Channel ch, Throwable exception) {
94-
System.err.println("Caught an exception when recovering channel " + ch.getChannelNumber());
95-
exception.printStackTrace(System.err);
82+
log("Caught an exception when recovering channel " + ch.getChannelNumber(), exception);
9683
}
9784

9885
/**
9986
* @since 3.3.0
10087
*/
10188
public void handleTopologyRecoveryException(Connection conn, Channel ch, TopologyRecoveryException exception) {
102-
System.err.println("Caught an exception when recovering topology " + exception.getMessage());
103-
exception.printStackTrace(System.err);
89+
log("Caught an exception when recovering topology " + exception.getMessage(), exception);
10490
}
10591

10692
protected void handleChannelKiller(Channel channel, Throwable exception, String what) {
107-
System.err.println(this.getClass().getName() + ": " + what + " threw an exception for channel "
108-
+ channel + ":");
109-
exception.printStackTrace();
93+
log(what + "threw an exception for channel "+channel, exception);
11094
}
11195

11296
protected void handleConnectionKiller(Connection connection, Throwable exception, String what) {
113-
// TODO: log the exception
114-
System.err.println("DefaultExceptionHandler: " + what + " threw an exception for connection "
115-
+ connection + ":");
116-
exception.printStackTrace();
97+
log(what + " threw an exception for connection " + connection, exception);
11798
try {
11899
connection.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + what);
119100
} catch (AlreadyClosedException ace) {
120101
// noop
121102
} catch (IOException ioe) {
122-
// TODO: log the failure
123-
System.err.println("Failure during close of connection " + connection + " after " + exception
124-
+ ":");
125-
ioe.printStackTrace();
103+
log("Failure during close of connection " + connection + " after " + exception, ioe);
126104
connection.abort(AMQP.INTERNAL_ERROR, "Internal error closing connection for " + what);
127105
}
128106
}
107+
108+
protected void log(String message, Throwable e) {
109+
LoggerFactory.getLogger(ForgivingExceptionHandler.class).error(
110+
message, e
111+
);
112+
}
129113
}

src/main/java/com/rabbitmq/client/impl/StrictExceptionHandler.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@
1515

1616
package com.rabbitmq.client.impl;
1717

18+
import com.rabbitmq.client.*;
19+
import org.slf4j.LoggerFactory;
20+
1821
import java.io.IOException;
19-
import java.net.ConnectException;
2022
import java.util.concurrent.TimeoutException;
2123

22-
import com.rabbitmq.client.AMQP;
23-
import com.rabbitmq.client.AlreadyClosedException;
24-
import com.rabbitmq.client.Channel;
25-
import com.rabbitmq.client.Connection;
26-
import com.rabbitmq.client.Consumer;
27-
import com.rabbitmq.client.ExceptionHandler;
28-
import com.rabbitmq.client.TopologyRecoveryException;
29-
3024
/**
3125
* An implementation of {@link com.rabbitmq.client.ExceptionHandler} that does
3226
* close channels on unhandled consumer exception.
@@ -64,21 +58,22 @@ public void handleConsumerException(Channel channel, Throwable exception,
6458
}
6559

6660
protected void handleChannelKiller(Channel channel, Throwable exception, String what) {
67-
System.err.println(this.getClass().getName() + ": " + what + " threw an exception for channel "
68-
+ channel + ":");
69-
exception.printStackTrace();
61+
log(what + " threw an exception for channel " + channel, exception);
7062
try {
7163
channel.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + what);
7264
} catch (AlreadyClosedException ace) {
7365
// noop
7466
} catch (TimeoutException ace) {
7567
// noop
7668
} catch (IOException ioe) {
77-
// TODO: log the failure
78-
System.err.println("Failure during close of channel " + channel + " after " + exception
79-
+ ":");
80-
ioe.printStackTrace();
69+
log("Failure during close of channel " + channel + " after " + exception, ioe);
8170
channel.getConnection().abort(AMQP.INTERNAL_ERROR, "Internal error closing channel for " + what);
8271
}
8372
}
73+
74+
protected void log(String message, Throwable e) {
75+
LoggerFactory.getLogger(StrictExceptionHandler.class).error(
76+
message, e
77+
);
78+
}
8479
}

src/main/java/com/rabbitmq/tools/json/JSONUtil.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.rabbitmq.tools.json;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
1922
import java.beans.BeanInfo;
2023
import java.beans.IntrospectionException;
2124
import java.beans.Introspector;
@@ -30,6 +33,8 @@
3033
* Utility methods for working with JSON objects in Java.
3134
*/
3235
public class JSONUtil {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);
3338
/**
3439
* Uses reflection to fill public fields and Bean properties of
3540
* the target object from the source Map.
@@ -90,11 +95,11 @@ public static void tryFill(Object target, Map<String, Object> source) {
9095
try {
9196
fill(target, source);
9297
} catch (IntrospectionException ie) {
93-
ie.printStackTrace();
98+
LOGGER.error("Error in tryFill", ie);
9499
} catch (IllegalAccessException iae) {
95-
iae.printStackTrace();
100+
LOGGER.error("Error in tryFill", iae);
96101
} catch (InvocationTargetException ite) {
97-
ite.printStackTrace();
102+
LOGGER.error("Error in tryFill", ite);
98103
}
99104
}
100105
}

src/main/java/com/rabbitmq/utility/Utility.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public static <T extends Throwable & SensibleClone<T>> T fixStackTrace(T throwab
6969
return throwable;
7070
}
7171

72-
72+
/**
73+
*
74+
* @param throwable
75+
* @return
76+
* @deprecated use logging library instead for logging stack traces somewhere
77+
*/
7378
public static String makeStackTrace(Throwable throwable) {
7479
ByteArrayOutputStream baOutStream = new ByteArrayOutputStream();
7580
PrintStream printStream = new PrintStream(baOutStream, false);

0 commit comments

Comments
 (0)