Yet Another Logging Utility
package com.company.util.debug;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* Modul logowania
*
*/
public class Debug {
public final static int TRACE_DEPTH = 25;
public final static boolean LOG_INFO = true;
public final static boolean LOG_ERR = true;
public final static String THIS_PACKAGE = "com.company";
public final static int TRACE_DEPTH = 25;
public final static boolean LOG_INFO = true;
public final static boolean LOG_ERR = true;
@SuppressWarnings("unchecked")
public final static void println(String txt, Object thiz) {
if (LOG_INFO) {
Exception e = new Exception();
StringBuffer msg = new StringBuffer();
msg.append("[").append(new Date()).append("] [Caller: ");
if (thiz instanceof Class)
msg.append( ((Class)thiz).getName() ).append("@static=");
else
msg.append( thiz.getClass().getName() ).append("@");
msg.append( thiz.hashCode() ).append("]");
msg.append(" th").append(Thread.currentThread().getId());
String thName = Thread.currentThread().getName();
if (thName!=null)
msg.append("=").append(thName);
msg.append("\r\n\tat ").append(e.getStackTrace()[1]);
msg.append("\r\n\tINFO: ").append(txt).append("\r\n");
System.out.println(msg);
}
}
public final static StringBuffer describeException(StringBuffer msg, Throwable err) {
msg.append("\r\n\tERR: ").append(err.toString()).append("\r\n");
StackTraceElement[] st = err.getStackTrace();
int n = (st!=null) ? Math.min(st.length, TRACE_DEPTH) : 0;
String prev = "";
for (int i=0; i < sti =" st[i]+"> ").append(st[i]);
else
msg.append("\r\n\t").append(st[i]);
prev = sti;
}
if (n>0)
msg.append("\r\n");
return msg;
}
@SuppressWarnings("unchecked")
public final static void printlnErr(String txt, Throwable ex, Object thiz) {
if (LOG_ERR) {
Exception e = new Exception();
StringBuffer msg = new StringBuffer();
msg.append("[").append(new Date()).append("] [Caller: ");
if (thiz instanceof Class)
msg.append( ((Class)thiz).getName() ).append("@static=");
else
msg.append( thiz.getClass().getName() ).append("@");
msg.append( thiz.hashCode() ).append("]");
msg.append("\r\n\tat ").append(e.getStackTrace()[1]);
msg.append("\r\n\tERR: ").append(txt);
Throwable t = ex;
while (t!=null) {
describeException(msg, t);
t = t.getCause();
}
System.out.println(msg);
}
}
@SuppressWarnings("unchecked")
public final static String describeErr(String txt, Throwable ex, Object thiz) {
if (LOG_ERR) {
Exception e = new Exception();
StringBuffer msg = new StringBuffer();
msg.append("[").append(new Date()).append("] [Caller: ");
if (thiz instanceof Class)
msg.append( ((Class)thiz).getName() ).append("@static=");
else
msg.append( thiz.getClass().getName() ).append("@");
msg.append( thiz.hashCode() ).append("]");
msg.append("\r\n\tat ").append(e.getStackTrace()[1]);
msg.append("\r\n\tERR: ").append(txt);
Throwable t = ex;
while (t!=null) {
describeException(msg, t);
t = t.getCause();
}
return msg.toString();
}
return ex.getLocalizedMessage();
}
}
0 komentarze:
Prześlij komentarz