[slf4j-dev] svn commit: r1258 - slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation

ravn at slf4j.org ravn at slf4j.org
Sun Dec 21 20:08:06 CET 2008


Author: ravn
Date: Sun Dec 21 20:08:06 2008
New Revision: 1258

Added:
   slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java

Log:
Now is robust against exceptions in parameter rendering by
keeping a WeakHashMap of those classes which cannot be rendered.



Added: slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-ext/src/main/java/org/slf4j/instrumentation/ToStringHelper.java	Sun Dec 21 20:08:06 2008
@@ -0,0 +1,25 @@
+package org.slf4j.instrumentation;
+
+import java.util.Map;
+import java.util.WeakHashMap;
+
+public class ToStringHelper {
+
+	final static Map<Class, Object> unrenderableClasses = new WeakHashMap<Class, Object>();
+
+	public static String render(Object o) {
+		if (o == null) {
+			return String.valueOf(o);
+		}
+		Class objectClass = o.getClass();
+		if (unrenderableClasses.containsKey(objectClass) == false) {
+			try {
+				return o.toString();
+			} catch (Exception e) {
+				Long now = new Long(System.currentTimeMillis());
+				unrenderableClasses.put(objectClass, now);
+			}
+		}
+		return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
+	}
+}



More information about the slf4j-dev mailing list