在写代码时,经常打印一些信息用于调试,我经常使用 System.out.println()
打印,但是有一个问题,打印的信息多的时候就很难找到输出的位置,为了方便找到打印的位置,写了个简易的Log类,以后有机会在添加功能.
Java简易的Log类如下:
public class Log {
private static final char TOP_LEFT_CORNER = '╔';
private static final char BOTTOM_LEFT_CORNER = '╚';
private static final char HORIZONTAL_DOUBLE_LINE = '║';
private static final String DOUBLE_DIVIDER = "════════════════════════════════════════════";
private static final String TOP_BORDER = TOP_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private static final String BOTTOM_BORDER = BOTTOM_LEFT_CORNER + DOUBLE_DIVIDER + DOUBLE_DIVIDER;
private Log(){}
private static String createInfo(String msg){
StringBuffer buffer = new StringBuffer();
StackTraceElement element = new Throwable().getStackTrace()[2];
String className = element.getClassName();
String methodName = element.getMethodName();
int lineNum = element.getLineNumber();
buffer.append(HORIZONTAL_DOUBLE_LINE);
buffer.append(" ");
buffer.append(className);
buffer.append("[");
buffer.append(methodName);
buffer.append(":");
buffer.append(lineNum);
buffer.append("]");
buffer.append(msg);
return buffer.toString();
}
public static void info(String msg){
System.out.println(TOP_BORDER);
System.out.println(HORIZONTAL_DOUBLE_LINE);
System.out.println(createInfo(msg));
System.out.println(HORIZONTAL_DOUBLE_LINE);
System.out.println(BOTTOM_BORDER);
}
}
测试类如下:
public class Client {
public static void main(String[] args) {
Log.info("123");
Log.info("测试...");
}
}
显示结果如下:
重要说明
想随时获取最新博客文章更新,请关注公共账号DevWiki,或扫描下面的二维码:
评论区