步骤 2 : 使用办法
LogPrintStream
package util;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LogPrintStream extends PrintStream{
public static boolean log = true;
public void println() {
println("");
}
public void println(boolean x) {
println(String.valueOf(x));
}
public void println(char x) {
println(String.valueOf(x));
}
public void println(int x) {
println(String.valueOf(x));
}
public void println(long x) {
println(String.valueOf(x));
}
public void println(float x) {
println(String.valueOf(x));
}
public void println(double x) {
println(String.valueOf(x));
}
public void println(char[] x) {
println(String.valueOf(x));
}
public LogPrintStream(OutputStream out) {
super(out);
}
public LogPrintStream() {
this(System.out);
}
public static void closeLog(){
log= false;
}
public void println(Object x){
println(String.valueOf(x));
}
public void println(String msg){
if(!log){
super.println(msg);
return;
}
try {
throw new Exception();
} catch (Exception e) {
StackTraceElement[] stes = e.getStackTrace();
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
StackTraceElement s= getTriggerTraceElement(stes);
String logFormat = "%s (%s:%d) %s() - %s%n";
super.printf(logFormat, time,s.getFileName(), s.getLineNumber(), s.getMethodName(), msg);
}
}
private StackTraceElement getTriggerTraceElement(StackTraceElement[] stes) {
for (StackTraceElement s : stes) {
String clazzName = s.getClassName();
if(clazzName.equals(LogPrintStream.class.getName()) ){
continue;
}
return s;
}
return null;
}
public static void init() {
System.setOut(new LogPrintStream());
}
}
在代码最开始的地方加上这么一句
LogPrintStream.init();
LogPrintStream.init();
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|