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文件中

*/ 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文件中

*/private static final boolean isWrite = false;/** * isDebug :是用来控制,是否打印日志 */private static final boolean isDeBug = true; 把这两个变量改成false,就可以停止日志的打印,这个类还可配合UncaughtExceptionHandler,来记录未捕获的全局异常,写入log文件,以便开发者寻找异常原因,来经行修改应用。

你可能感兴趣的:(Android工具类)