主类:
package util;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
public class Log {
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static List<Loghandler>loghandlerList;
private Log() {}
public static void e(String title,String content) {
if(loghandlerList==null)
loghandlerList=new LinkedList<Loghandler>();
//console输出
String out="\n"+title+"-->"+content+"\n TIME:"+getCurrentTIme()+"\n";
System.out.print(out);
//console输出end
}
public static void e_Interface(String title,String content) {
content+="\nTIME:"+getCurrentTIme();
for(int i=0;i<loghandlerList.size();i++)
loghandlerList.get(i).out(title,content);
}
public static void attach(Loghandler handler) {
if(loghandlerList==null)
loghandlerList=new LinkedList<Loghandler>();
loghandlerList.add(handler);
}
public static void dettach(Loghandler handler) {
loghandlerList.remove(handler);
}
public static String getCurrentTIme() {
return df.format(System.currentTimeMillis());
}
}
辅助接口:
package util;
public interface Loghandler {//消息输出接口
public void out(String title,String content);
}