From a2a7531632fcad9f275bc33f6c2f5dbcc1d0ecd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E7=8E=89=E6=A1=94?= <769213327@qq.com> Date: Mon, 14 Jan 2019 23:21:29 +0800 Subject: [PATCH] Optimize the code: use logger instead of printStackTrace(). (#3202) --- .../java/org/apache/dubbo/common/utils/PojoUtils.java | 11 ++++++++--- .../main/java/org/apache/dubbo/container/Main.java | 1 - .../transport/netty4/logging/MessageFormatter.java | 6 +++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index e9a9c63201a..2198709fb73 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -16,6 +16,9 @@ */ package org.apache.dubbo.common.utils; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; + import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -57,6 +60,7 @@ */ public class PojoUtils { + private static final Logger logger = LoggerFactory.getLogger(PojoUtils.class); private static final ConcurrentMap NAME_METHODS_CACHE = new ConcurrentHashMap(); private static final ConcurrentMap, ConcurrentMap> CLASS_FIELD_CACHE = new ConcurrentHashMap, ConcurrentMap>(); @@ -457,9 +461,10 @@ private static Object realize0(Object pojo, Class type, Type genericType, fin try { method.invoke(dest, value); } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name - + " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage(), e); + String exceptionDescription = "Failed to set pojo " + dest.getClass().getSimpleName() + " property " + name + + " value " + value + "(" + value.getClass() + "), cause: " + e.getMessage(); + logger.error(exceptionDescription, e); + throw new RuntimeException(exceptionDescription, e); } } else if (field != null) { value = realize0(value, field.getType(), field.getGenericType(), history); diff --git a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java index 80646ae7bb9..5462d5215ce 100644 --- a/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java +++ b/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/container/Main.java @@ -88,7 +88,6 @@ public void run() { } System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!"); } catch (RuntimeException e) { - e.printStackTrace(); logger.error(e.getMessage(), e); System.exit(1); } diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java index f860875bce1..9bc3ff99890 100644 --- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java +++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/logging/MessageFormatter.java @@ -16,6 +16,9 @@ */ package org.apache.dubbo.remoting.transport.netty4.logging; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; + import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; @@ -86,6 +89,7 @@ * {@link #arrayFormat(String, Object[])} methods for more details. */ final class MessageFormatter { + private static final Logger logger = LoggerFactory.getLogger(MessageFormatter.class); static final char DELIM_START = '{'; static final char DELIM_STOP = '}'; static final String DELIM_STR = "{}"; @@ -279,7 +283,7 @@ private static void safeObjectAppend(StringBuffer sbuf, Object o) { System.err .println("SLF4J: Failed toString() invocation on an object of type [" + o.getClass().getName() + ']'); - t.printStackTrace(); + logger.error(t.getMessage(), t); sbuf.append("[FAILED toString()]"); } }