android開發------------------Log日誌工具類(LogUtils)

 

博客爲 有時個哥 原創,如需轉載請標明出處:http://blog.csdn.net/ls703/article/details/42973553

在應用開發中,我們需要常加一些日誌打印來做調試,現在給出一個日誌工具類

package ls.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;

import com.config.AppConfig;

/**
 * Title: LogUtils.java Description: 日誌工具類:開發過程中,日誌輸出
 * 
 * @author song
 * @date 2014-9-9 下午1:22:55
 * @version V1.0
 */
public class LogUtils {
	/**
	 * isWrite:用於開關是否吧日誌寫入txt文件中</p>
	 */
	private static final boolean isWrite = false;
	/**
	 * isDebug :是用來控制,是否打印日誌
	 */
	private static final boolean isDeBug = true;
	/**
	 * 存放日誌文件的所在路徑
	 */
	private static final String DIRPATH = AppConfig.LOG_DIRPATH;
	// private static final String DIRPATH = "/log";
	/**
	 * 存放日誌的文本名
	 */
	private static final String LOGNAME = AppConfig.LOG_FILENAME;
	// private static final String LOGNAME = "log.txt";
	/**
	 * 設置時間的格式
	 */
	private static final String INFORMAT = "yyyy-MM-dd HH:mm:ss";
	/**
	 * VERBOSE日誌形式的標識符
	 */
	public static final int VERBOSE = 5;
	/**
	 * DEBUG日誌形式的標識符
	 */
	public static final int DEBUG = 4;
	/**
	 * INFO日誌形式的標識符
	 */
	public static final int INFO = 3;
	/**
	 * WARN日誌形式的標識符
	 */
	public static final int WARN = 2;
	/**
	 * ERROR日誌形式的標識符
	 */
	public static final int ERROR = 1;

	/**
	 * 把異常用來輸出日誌的綜合方法
	 * 
	 * @param @param tag 日誌標識
	 * @param @param throwable 拋出的異常
	 * @param @param type 日誌類型
	 * @return void 返回類型
	 * @throws
	 */
	public static void log(String tag, Throwable throwable, int type) {
		log(tag, exToString(throwable), type);
	}

	/**
	 * 用來輸出日誌的綜合方法(文本內容)
	 * 
	 * @param @param tag 日誌標識
	 * @param @param msg 要輸出的內容
	 * @param @param type 日誌類型
	 * @return void 返回類型
	 * @throws
	 */
	public static void log(String tag, String msg, int type) {
		switch (type) {
		case VERBOSE:
			v(tag, msg);// verbose等級
			break;
		case DEBUG:
			d(tag, msg);// debug等級
			break;
		case INFO:
			i(tag, msg);// info等級
			break;
		case WARN:
			w(tag, msg);// warn等級
			break;
		case ERROR:
			e(tag, msg);// error等級
			break;
		default:
			break;
		}
	}

	/**
	 * verbose等級的日誌輸出
	 * 
	 * @param tag
	 *            日誌標識
	 * @param msg
	 *            要輸出的內容
	 * @return void 返回類型
	 * @throws
	 */
	public static void v(String tag, String msg) {
		// 是否開啓日誌輸出
		if (isDeBug) {
			Log.v(tag, msg);
		}
		// 是否將日誌寫入文件
		if (isWrite) {
			write(tag, msg);
		}
	}

	/**
	 * debug等級的日誌輸出
	 * 
	 * @param tag
	 *            標識
	 * @param msg
	 *            內容
	 * @return void 返回類型
	 * @throws
	 */
	public static void d(String tag, String msg) {
		if (isDeBug) {
			Log.d(tag, msg);
		}
		if (isWrite) {
			write(tag, msg);
		}
	}

	/**
	 * info等級的日誌輸出
	 * 
	 * @param  tag 標識
	 * @param  msg 內容
	 * @return void 返回類型
	 * @throws
	 */
	public static void i(String tag, String msg) {
		if (isDeBug) {
			Log.i(tag, msg);
		}
		if (isWrite) {
			write(tag, msg);
		}
	}

	/**
	 * warn等級的日誌輸出
	 * 
	 * @param tag 標識
	 * @param msg 內容
	 * @return void 返回類型
	 * @throws
	 */
	public static void w(String tag, String msg) {
		if (isDeBug) {
			Log.w(tag, msg);
		}
		if (isWrite) {
			write(tag, msg);
		}
	}

	/**
	 * error等級的日誌輸出
	 * 
	 * @param  tag 標識
	 * @param  msg 內容
	 * @return void 返回類型
	 */
	public static void e(String tag, String msg) {
		if (isDeBug) {
			Log.w(tag, msg);
		}
		if (isWrite) {
			write(tag, msg);
		}
	}

	/**
	 * 用於把日誌內容寫入制定的文件
	 * 
	 * @param @param tag 標識
	 * @param @param msg 要輸出的內容
	 * @return void 返回類型
	 * @throws
	 */
	public static void write(String tag, String msg) {
		String path = FileUtils.createMkdirsAndFiles(DIRPATH, LOGNAME);
		if (TextUtils.isEmpty(path)) {
			return;
		}
		String log = DateFormat.format(INFORMAT, System.currentTimeMillis())
				+ tag
				+ "========>>"
				+ msg
				+ "\n=================================分割線=================================";
		FileUtils.write2File(path, log, true);
	}

	/**
	 * 用於把日誌內容寫入制定的文件
	 * 
	 * @param tag
	 *            標籤
	 * @param ex
	 *            異常
	 */
	public static void write(Throwable ex) {
		write("", exToString(ex));
	}

	/**
	 * 把異常信息轉化爲字符串
	 * 
	 * @param ex 異常信息
	 * @return 異常信息字符串
	 */
	private static String exToString(Throwable ex) {
		Writer writer = new StringWriter();
		PrintWriter printWriter = new PrintWriter(writer);
		ex.printStackTrace(printWriter);
		printWriter.close();
		String result = writer.toString();
		return result;
	}
}


其中的AppConfig.LOG_DIRPATH;和AppConfig.LOG_FILENAME;是我在配置常量類中設置的存放log日誌的文件路徑和文件名,可自己定義;

FileUtils.createMkdirsAndFiles(DIRPATH, LOGNAME);是另一個工具類,隨還沒補充完全,但這裏是可以用的,以後補充後在給詳細的,現在先給出現在的,以便大家使用

package ls.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.text.TextUtils;
/**
 * 
 * Title: FileUtils.java
 * Description: 對sd卡的文件相關操作
 * @author Liusong
 * @date 2015-1-12
 * @version V1.0
 */
public class FileUtils {

	/**
	 * 判斷sdcrad是否已經安裝
	 * @return boolean true安裝 false 未安裝
	 */
	public static boolean isSDCardMounted(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
	
	/**
	 * 得到sdcard的路徑
	 * @return
	 */
	public static String getSDCardRoot(){
		System.out.println(isSDCardMounted()+Environment.getExternalStorageState());
		if(isSDCardMounted()){
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return "";
	}
	/**
	 * 創建文件的路徑及文件
	 * @param path 路徑,方法中以默認包含了sdcard的路徑,path格式是"/path...."
	 * @param filename 文件的名稱
	 * @return 返回文件的路徑,創建失敗的話返回爲空
	 */
	public static String createMkdirsAndFiles(String path, String filename) {
		if (TextUtils.isEmpty(path)) {
			throw new RuntimeException("路徑爲空");
		}
		path = getSDCardRoot()+path;
		File file = new File(path);
		if (!file.exists()) {
			try {
				file.mkdirs();
			} catch (Exception e) {
				throw new RuntimeException("創建文件夾不成功");
			}
		} 
		File f = new File(file, filename);
		if(!f.exists()){
			try {
				f.createNewFile();
			} catch (IOException e) {
				throw new RuntimeException("創建文件不成功");
			}
		}
		return f.getAbsolutePath();
	}
	
	/**
	 * 把內容寫入文件
	 * @param path 文件路徑
	 * @param text 內容
	 */
	public static void write2File(String path,String text,boolean append){
		BufferedWriter bw = null;
		try {
			//1.創建流對象
			bw = new BufferedWriter(new FileWriter(path,append));
			//2.寫入文件
			bw.write(text);
			//換行刷新
			bw.newLine();
			bw.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//4.關閉流資源
			if(bw!= null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 刪除文件
	 * @param path
	 * @return
	 */
	public static boolean deleteFile(String path){
		if(TextUtils.isEmpty(path)){
			throw new RuntimeException("路徑爲空");
		}
		File file = new File(path);
		if(file.exists()){
			try {
				file.delete();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}


logUtils的使用個正常的一樣使用,其中有兩個變量來控制是否打印日誌,這是避免,開發完成後,我們不可能把日誌輸出語句一個一個的去關閉,所以只要我們把

/** * isWrite:用於開關是否吧日誌寫入txt文件中</p> */private static final boolean isWrite = false;/** * isDebug :是用來控制,是否打印日誌 */private static final boolean isDeBug = true; 把這兩個變量改成false,就可以停止日誌的打印,這個類還可配合UncaughtExceptionHandler,來記錄未捕獲的全局異常,寫入log文件,以便開發者尋找異常原因,來經行修改應用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章