Skip to content

Commit a2a7531

Browse files
zhaoyuguangkhanimteyaz
authored andcommitted
Optimize the code: use logger instead of printStackTrace(). (apache#3202)
1 parent dfad45d commit a2a7531

File tree

3 files changed

+13
-5
lines changed
  • dubbo-common/src/main/java/org/apache/dubbo/common/utils
  • dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container
  • dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging

3 files changed

+13
-5
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.dubbo.common.utils;
1818

19+
import org.apache.dubbo.common.logger.Logger;
20+
import org.apache.dubbo.common.logger.LoggerFactory;
21+
1922
import java.lang.reflect.Array;
2023
import java.lang.reflect.Constructor;
2124
import java.lang.reflect.Field;
@@ -57,6 +60,7 @@
5760
*/
5861
public class PojoUtils {
5962

63+
private static final Logger logger = LoggerFactory.getLogger(PojoUtils.class);
6064
private static final ConcurrentMap<String, Method> NAME_METHODS_CACHE = new ConcurrentHashMap<String, Method>();
6165
private static final ConcurrentMap<Class<?>, ConcurrentMap<String, Field>> CLASS_FIELD_CACHE = new ConcurrentHashMap<Class<?>, ConcurrentMap<String, Field>>();
6266

@@ -457,9 +461,10 @@ private static Object realize0(Object pojo, Class<?> type, Type genericType, fin
457461
try {
458462
method.invoke(dest, value);
459463
} catch (Exception e) {
460-
e.printStackTrace();
461-
throw new RuntimeException("Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name
462-
+ " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage(), e);
464+
String exceptionDescription = "Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name
465+
+ " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage();
466+
logger.error(exceptionDescription, e);
467+
throw new RuntimeException(exceptionDescription, e);
463468
}
464469
} else if (field != null) {
465470
value = realize0(value, field.getType(), field.getGenericType(), history);

dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public void run() {
8888
}
8989
System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!");
9090
} catch (RuntimeException e) {
91-
e.printStackTrace();
9291
logger.error(e.getMessage(), e);
9392
System.exit(1);
9493
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.dubbo.remoting.transport.netty4.logging;
1818

19+
import org.apache.dubbo.common.logger.Logger;
20+
import org.apache.dubbo.common.logger.LoggerFactory;
21+
1922
import java.text.MessageFormat;
2023
import java.util.HashMap;
2124
import java.util.Map;
@@ -86,6 +89,7 @@
8689
* {@link #arrayFormat(String, Object[])} methods for more details.
8790
*/
8891
final class MessageFormatter {
92+
private static final Logger logger = LoggerFactory.getLogger(MessageFormatter.class);
8993
static final char DELIM_START = '{';
9094
static final char DELIM_STOP = '}';
9195
static final String DELIM_STR = "{}";
@@ -279,7 +283,7 @@ private static void safeObjectAppend(StringBuffer sbuf, Object o) {
279283
System.err
280284
.println("SLF4J: Failed toString() invocation on an object of type ["
281285
+ o.getClass().getName() + ']');
282-
t.printStackTrace();
286+
logger.error(t.getMessage(), t);
283287
sbuf.append("[FAILED toString()]");
284288
}
285289
}

0 commit comments

Comments
 (0)