Skip to content

Commit

Permalink
Optimize the code: use logger instead of printStackTrace(). (apache#3202
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zhaoyuguang authored and khanimteyaz committed Jan 18, 2019
1 parent dfad45d commit a2a7531
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +60,7 @@
*/
public class PojoUtils {

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

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "{}";
Expand Down Expand Up @@ -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()]");
}
}
Expand Down

0 comments on commit a2a7531

Please sign in to comment.