源碼下載:http://www.eoeandroid.com/thread-587291-1-1.html
開發應用的時候,我們一般情況下只能在開發的時候可以看到logcat裏面的日誌,但是我們在日常使用應用的時候,不能時時都連電腦看bug,所以我們最好就是有一個 日誌文件功能 可以將日誌保存到 手機內存卡,當我們的應用 出現問題時,我們就可以查看內存卡里的 日誌文件,這樣我們就方便查看我們的應用 出現的問題。
1. 如果要添加日誌到手機內存卡,首先要往 app 添加權限<!-- 往sdcard中寫入數據的權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- 在sdcard中創建/刪除文件的權限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
2.編寫日誌文件類
-
package com.happy.logger;
-
-
import java.io.File;
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
import java.util.Date;
-
import java.util.Hashtable;
-
-
import org.apache.log4j.Logger;
-
-
import android.content.Context;
-
-
import com.happy.common.Constants;
-
-
import de.mindpipe.android.logging.log4j.LogConfigurator;
-
-
public class LoggerManage {
-
/**
-
* 是否輸出log
-
*/
-
private final static boolean logFlag = true;
-
-
/**
-
* 應用名標籤
-
*/
-
public final static String tag = "[" + Constants.APPNAME + "]";
-
private String userName = null;
-
/**
-
* 保存用戶對應的Logger
-
*/
-
private static Hashtable<String, LoggerManage> sLoggerTable = new Hashtable<String, LoggerManage>();
-
-
private static int SDCARD_LOG_FILE_SAVE_DAYS = 3;// sd卡中日誌文件的最多保存天數
-
private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日誌文件格式
-
private static Logger logger;
-
private static LogConfigurator logConfigurator;
-
-
public LoggerManage(String userName) {
-
this.userName = userName;
-
if (logger == null) {
-
initLog();
-
logger = Logger.getLogger(tag);
-
}
-
}
-
-
/**
-
* 初始化log的相關配置
-
*/
-
private void initLog() {
-
logConfigurator = new LogConfigurator();
-
-
String time = logfile.format(new Date());
-
String fileName = time + ".log";
-
String path = Constants.PATH_LOGCAT + File.separator + fileName;
-
logConfigurator.setFileName(path);
-
// 輸出內容的格式
-
logConfigurator.setFilePattern("%d{yyyy-MM-dd HH:mm:ss} <%m>%n%n");
-
// 文件大小500k
-
logConfigurator.setMaxFileSize(1024 * 500);
-
// 備份
-
logConfigurator.setMaxBackupSize(1);
-
// 表示所有消息都會被立即輸出,設爲false則不輸出,默認值是true。
-
logConfigurator.setImmediateFlush(true);
-
logConfigurator.configure();
-
-
cleanOldLogFile();
-
-
}
-
-
/**
-
* 清除過期的log文件
-
*/
-
private void cleanOldLogFile() {
-
File logFileParent = new File(Constants.PATH_LOGCAT);
-
if (logFileParent.exists()) {
-
String needDelTime = logfile.format(getDateBefore());
-
File[] files = logFileParent.listFiles();
-
if (files != null) {
-
for (int i = 0; i < files.length; i++) {
-
String fileName = files[i].getName();
-
fileName = fileName.substring(0, fileName.lastIndexOf("."));
-
if (needDelTime.compareTo(fileName) > 0) {
-
files[i].delete();
-
}
-
}
-
}
-
}
-
}
-
-
/**
-
* 得到現在時間前的幾天日期,用來得到需要刪除的日誌文件名
-
* */
-
private Date getDateBefore() {
-
Date nowtime = new Date();
-
Calendar now = Calendar.getInstance();
-
now.setTime(nowtime);
-
now.set(Calendar.DATE, now.get(Calendar.DATE)
-
- SDCARD_LOG_FILE_SAVE_DAYS);
-
return now.getTime();
-
}
-
-
/**
-
*
-
* @param userName
-
* 用戶名
-
* @return
-
*/
-
public static LoggerManage getLogger(String userName) {
-
LoggerManage userLogger = (LoggerManage) sLoggerTable.get(userName);
-
if (userLogger == null) {
-
userLogger = new LoggerManage(userName);
-
sLoggerTable.put(userName, userLogger);
-
}
-
return userLogger;
-
-
}
-
-
/**
-
* 創建zhangliangming用法
-
*
-
* @return
-
*/
-
public static LoggerManage getZhangLogger(Context mContext) {
-
String name = "zhangliangming";
-
LoggerManage userLogger = (LoggerManage) sLoggerTable.get(name);
-
if (userLogger == null) {
-
userLogger = new LoggerManage(name);
-
sLoggerTable.put(name, userLogger);
-
}
-
return userLogger;
-
}
-
-
/**
-
* 獲取方法名
-
*
-
* @return
-
*/
-
private String getFunctionName() {
-
StackTraceElement[] sts = Thread.currentThread().getStackTrace();
-
if (sts == null) {
-
return null;
-
}
-
for (StackTraceElement st : sts) {
-
if (st.isNativeMethod()) {
-
continue;
-
}
-
if (st.getClassName().equals(Thread.class.getName())) {
-
continue;
-
}
-
if (st.getClassName().equals(this.getClass().getName())) {
-
continue;
-
}
-
return "@" + userName + "[url=home.php?mod=space&uid=2779]@[/url] " + "[ "
-
+ Thread.currentThread().getName() + ": "
-
+ st.getClassName() + ":" + st.getLineNumber() + " "
-
+ st.getMethodName() + " ]";
-
}
-
return null;
-
}
-
-
public void i(String str) {
-
if (logFlag) {
-
String name = getFunctionName();
-
if (name != null) {
-
logger.info(name + " - " + str);
-
} else {
-
logger.info(str.toString());
-
}
-
}
-
}
-
-
public void d(String str) {
-
if (logFlag) {
-
String name = getFunctionName();
-
if (name != null) {
-
logger.debug(name + " - " + str);
-
} else {
-
logger.debug(str.toString());
-
}
-
}
-
}
-
-
public void f(String str) {
-
if (logFlag) {
-
String name = getFunctionName();
-
if (name != null) {
-
logger.fatal(name + " - " + str);
-
} else {
-
logger.fatal(str.toString());
-
}
-
}
-
}
-
-
public void w(String str) {
-
if (logFlag) {
-
String name = getFunctionName();
-
if (name != null) {
-
logger.warn(name + " - " + str);
-
} else {
-
logger.warn(str.toString());
-
}
-
}
-
}
-
-
public void e(String str) {
-
if (logFlag) {
-
String name = getFunctionName();
-
if (name != null) {
-
logger.error(name + " - " + str);
-
} else {
-
logger.error(str.toString());
-
}
-
}
-
}
-
-
}