代碼如下:
首先是Logger,它是一個接口,它提供了日志的記錄器所要做的一些事情.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
/**
* 一個日志生成器要實現的接口
* @author hadeslee
*/
public interface Logger {
public static final int FINE = 0;
public static final int INFO = 10;
public static final int DEBUG = 20;
public static final int WARNING = 30;
public static final int ERROR = 40;
/**
* 實現的log方法
* @param level 級別
* @param info 內容
*/
public void log(int level, String info);
/**
* 得到所有的日志內容
* @return 內容
*/
public String getLogContent();
/**
* 清除當前的日志
*/
public void clearLog();
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
/**
* 一個日志生成器要實現的接口
* @author hadeslee
*/
public interface Logger {
public static final int FINE = 0;
public static final int INFO = 10;
public static final int DEBUG = 20;
public static final int WARNING = 30;
public static final int ERROR = 40;
/**
* 實現的log方法
* @param level 級別
* @param info 內容
*/
public void log(int level, String info);
/**
* 得到所有的日志內容
* @return 內容
*/
public String getLogContent();
/**
* 清除當前的日志
*/
public void clearLog();
}
然后是日志顯示器,因為日志記錄了之后,肯定是要被我們顯示出來的,想要如何顯示,可以實現此接口.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
/**
* 一個用于顯示日志的接口,此接口用于LogManager來調用
* @author hadeslee
*/
public interface LogShower {
/**
* 顯示日志,由LogManager調用此方法來顯示日志
* 顯示日志可以有多種方法,比如可以用列表來顯示
* 也可以用TextArea來顯示,還可以用Canvas來顯示
* @param logContent 日志內容
* @param action 返回的時候要做的動作
*/
public void showLog(String logContent, BackAction action);
/**
* 內部的一個靜態接口,實現此接口以供LogShower在
* 點擊了返回之后,要做的事情
*/
public static interface BackAction {
/**
* 點擊返回之后要做的事情
*/
public void back();
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
/**
* 一個用于顯示日志的接口,此接口用于LogManager來調用
* @author hadeslee
*/
public interface LogShower {
/**
* 顯示日志,由LogManager調用此方法來顯示日志
* 顯示日志可以有多種方法,比如可以用列表來顯示
* 也可以用TextArea來顯示,還可以用Canvas來顯示
* @param logContent 日志內容
* @param action 返回的時候要做的動作
*/
public void showLog(String logContent, BackAction action);
/**
* 內部的一個靜態接口,實現此接口以供LogShower在
* 點擊了返回之后,要做的事情
*/
public static interface BackAction {
/**
* 點擊返回之后要做的事情
*/
public void back();
}
}
最后一個類就是LogManager,它只提供了靜態方法供調用,它內部有一個默認的Logger實現和一個默認的LogShower實現,在此類中的Shower實現可能不通過,因為我用到了LWUIT里面的一些組件,這個可以自行修改,添加自己的默認實現.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
import com.hadeslee.insurance.mobile.log.LogShower.BackAction;
import com.hadeslee.insurance.mobile.util.Util;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
/**
* 日志管理器,所有的日志通過此日志管理器
* 進行統一的調用
* 此類相關的兩個接口都有相應的默認實現,當然
* 也可以替換實現
* @author hadeslee
*/
public final class LogManager {
private static Logger log = new LoggerImpl();//具體的日志實現類
private static LogShower shower = new LogShowerImpl();//日志顯示者
private LogManager() {
}
/**
* 安裝自己實現的日志記錄器
* @param log 新的日志記錄器
*/
public static void install(Logger log) {
LogManager.log = log;
}
/**
* 安裝自己實現的日志顯示器
* @param shower 新的日志顯示器
*/
public static void install(LogShower shower) {
LogManager.shower = shower;
}
/**
* 記錄INFO級別的日志
* @param info 日志內容
*/
public static void info(String info) {
log.log(Logger.INFO, info);
}
/**
* 記錄DEBUG級別的日志
* @param info 日志內容
*/
public static void debug(String info) {
log.log(Logger.DEBUG, info);
}
/**
* 記錄ERROR級別的日志
* @param info 日志內容
*/
public static void error(String info) {
log.log(Logger.ERROR, info);
}
/**
* 記錄WARNING級別的日志
* @param info 日志內容
*/
public static void warning(String info) {
log.log(Logger.WARNING, info);
}
/**
* 記錄FINE級別的日志
* @param info 日志的內容
*/
public static void fine(String info) {
log.log(Logger.FINE, info);
}
/**
* 顯示當前日志管理器的日志
* @param back 要返回的時候,做的動作
*/
public static void showLog(BackAction back) {
shower.showLog(log.getLogContent(), back);
}
/**
* 清除當前日志管理器的日志
*/
public static void clearLog() {
log.clearLog();
}
static class LogShowerImpl implements LogShower {
public void showLog(String logContent, final BackAction action) {
Form form = new Form("日志內容");
form.setScrollable(false);
final TextArea ta = new TextArea(logContent, 5, 10);
ta.setEditable(false);
form.addCommand(new Command("返回") {
public void actionPerformed(ActionEvent ae) {
action.back();
}
});
form.addCommand(new Command("清除") {
public void actionPerformed(ActionEvent ae) {
LogManager.clearLog();
ta.setText("");
}
});
form.setLayout(new BorderLayout());
form.addComponent(BorderLayout.CENTER, ta);
form.show();
}
}
static class LoggerImpl implements Logger {
private StringBuffer sb;
public LoggerImpl() {
sb = new StringBuffer(1024);
}
public void log(int level, String info) {
sb.append(getPrefix()).append("\n").
append(getLevelName(level)).append(":").
append(info).append("\n");
}
private String getPrefix() {
return "[" + Thread.currentThread() + "-" + Util.getCurrentTime() + "]";
}
private String getLevelName(int level) {
switch (level) {
case FINE:
return "FINE";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
case WARNING:
return "WARNING";
case ERROR:
return "ERROR";
default:
return "UNKNOWN";
}
}
public String getLogContent() {
return sb.toString();
}
public void clearLog() {
sb.delete(0, sb.length());
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.insurance.mobile.log;
import com.hadeslee.insurance.mobile.log.LogShower.BackAction;
import com.hadeslee.insurance.mobile.util.Util;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
/**
* 日志管理器,所有的日志通過此日志管理器
* 進行統一的調用
* 此類相關的兩個接口都有相應的默認實現,當然
* 也可以替換實現
* @author hadeslee
*/
public final class LogManager {
private static Logger log = new LoggerImpl();//具體的日志實現類
private static LogShower shower = new LogShowerImpl();//日志顯示者
private LogManager() {
}
/**
* 安裝自己實現的日志記錄器
* @param log 新的日志記錄器
*/
public static void install(Logger log) {
LogManager.log = log;
}
/**
* 安裝自己實現的日志顯示器
* @param shower 新的日志顯示器
*/
public static void install(LogShower shower) {
LogManager.shower = shower;
}
/**
* 記錄INFO級別的日志
* @param info 日志內容
*/
public static void info(String info) {
log.log(Logger.INFO, info);
}
/**
* 記錄DEBUG級別的日志
* @param info 日志內容
*/
public static void debug(String info) {
log.log(Logger.DEBUG, info);
}
/**
* 記錄ERROR級別的日志
* @param info 日志內容
*/
public static void error(String info) {
log.log(Logger.ERROR, info);
}
/**
* 記錄WARNING級別的日志
* @param info 日志內容
*/
public static void warning(String info) {
log.log(Logger.WARNING, info);
}
/**
* 記錄FINE級別的日志
* @param info 日志的內容
*/
public static void fine(String info) {
log.log(Logger.FINE, info);
}
/**
* 顯示當前日志管理器的日志
* @param back 要返回的時候,做的動作
*/
public static void showLog(BackAction back) {
shower.showLog(log.getLogContent(), back);
}
/**
* 清除當前日志管理器的日志
*/
public static void clearLog() {
log.clearLog();
}
static class LogShowerImpl implements LogShower {
public void showLog(String logContent, final BackAction action) {
Form form = new Form("日志內容");
form.setScrollable(false);
final TextArea ta = new TextArea(logContent, 5, 10);
ta.setEditable(false);
form.addCommand(new Command("返回") {
public void actionPerformed(ActionEvent ae) {
action.back();
}
});
form.addCommand(new Command("清除") {
public void actionPerformed(ActionEvent ae) {
LogManager.clearLog();
ta.setText("");
}
});
form.setLayout(new BorderLayout());
form.addComponent(BorderLayout.CENTER, ta);
form.show();
}
}
static class LoggerImpl implements Logger {
private StringBuffer sb;
public LoggerImpl() {
sb = new StringBuffer(1024);
}
public void log(int level, String info) {
sb.append(getPrefix()).append("\n").
append(getLevelName(level)).append(":").
append(info).append("\n");
}
private String getPrefix() {
return "[" + Thread.currentThread() + "-" + Util.getCurrentTime() + "]";
}
private String getLevelName(int level) {
switch (level) {
case FINE:
return "FINE";
case INFO:
return "INFO";
case DEBUG:
return "DEBUG";
case WARNING:
return "WARNING";
case ERROR:
return "ERROR";
default:
return "UNKNOWN";
}
}
public String getLogContent() {
return sb.toString();
}
public void clearLog() {
sb.delete(0, sb.length());
}
}
}
以上的默認實現中,日志是記錄在內存中的,可以用clearLog方法把它清除,當然,也可以自定久記錄在RMS里面的日志,并且也要實現相關的clearLog的方法,添加這個方法是因為日志內容不可能讓它永遠無休止的增長.然后LogManager的showLog方法,就是利用LogShower的實現,把日志顯示出來,還有一點,顯示日志以后,為了能讓LogShower知道,如何返回上一個界面,這里還應該實現一個BackAction方法.
經過自己這幾天的使用,發現挺好用的,所以特此和大家分享一下,以提高在開發JAVAME的程序中的一些效率.
在WTK模擬器中的截圖如下 :

盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.