Andoird日志输入到SD卡中(包异常)

在做测试的时候遇到总是,开发人员总想在手机打印一些结果好查看一下总是原因或用于了解一些api作用等,

以及提供一个简单的日志,可以在将日志打印到sd卡中,并会打印在控制台。

(使用者可以增加一些其它级别方法控制各种打印级别,及控制是否打印日志)

public class LogUtils {
	private LogUtils() {

	}
	public static enum LogLevel implements Comparable<LogLevel> {
		NONE, ERROR, WARNING, INFO, DEBUG, VERBOSE;

		/**
		 * @Title: isSameOrLessThan
		 * @description 比较方法
		 * @author nWX142953
		 * @date 2012-8-29
		 * @param pLogLevel
		 * @return boolean
		 */
		public boolean isSameOrLessThan(final LogLevel pLogLevel) {
			return this.compareTo(pLogLevel) >= 0;
		}
	}

	/**
	 * @Fields sTag : Tag
	 */
	private static String sTag = "LogUtils";
	/**
	 * 日志级别设置
	 */
	private static LogLevel sLogLevel = LogLevel.WARNING;

	public static String getTag() {
		return LogUtils.sTag;
	}

	public static void setTag(final String pTag) {
		LogUtils.sTag = pTag;
	}

	public static LogLevel getDebugLevel() {
		return LogUtils.sLogLevel;
	}

	/**
	
	 * @description 设置日志级别
	
	 */
	public static void setLogLevel(final LogLevel pLogLevel) {
		if (pLogLevel == null) {
			throw new IllegalArgumentException("pLogLevel must not be null!");
		}
		LogUtils.sLogLevel = pLogLevel;
	}

	public static void v(final String pMessage) {
		LogUtils.v(LogUtils.sTag, pMessage, null);
	}


	public static void v(final String pMessage, final Throwable pThrowable) {
		LogUtils.v(LogUtils.sTag, pMessage, pThrowable);
	}


	public static void v(final String pTag, final String pMessage) {
		LogUtils.v(pTag, pMessage, null);
	}
	//调用系统打印
	public static void v(final String pTag, final String pMessage,
			final Throwable pThrowable) {
		if (LogUtils.sLogLevel.isSameOrLessThan(LogLevel.VERBOSE)) {
			if (pThrowable == null) {
				Log.v(pTag, pMessage);
			} else {
				Log.v(pTag, pMessage, pThrowable);
			}
		}
	}

	//将日志打印到sd卡里去
	public static void fv(final String pMessage) {
		f(LogUtils.sTag, pMessage, null);
	}

	public static void fv(final String pTag, final String pMessage) {
		f(pTag, pMessage, null);
	}

	// 输入到sD卡中
	public static void fv(final String pTag, final String pMessage,
			final Throwable pThrowable) {
			//同时输入到控制台
			v(pTag, pMessage, pThrowable);
			if (pThrowable == null) {
				writeFile("v", pTag, pMessage);
			} else {
				writeFile("v", pTag, pThrowable.getMessage());
			}
		}
	}

	/**
	 * 打开日志文件并写入日志
	 * 
	 * @return
	 * **/
	private static void writeFile(String mylogtype, String tag, String text) {// 新建或打开日志文件
		SimpleDateFormat logSF = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
		SimpleDateFormat msgSF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
		Date nowtime = new Date();
		String fileName = logSF.format(nowtime);
		String msg = msgSF.format(nowtime) + "    " + mylogtype
				+ "    " + tag + "    " + text;
		String path = Environment.getExternalStorageDirectory()
				+ "/Log/";
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(path, fileName + "Log.txt");
		try {
			FileWriter filerWriter = new FileWriter(file, true);// 后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖
			BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8"));
			bufWriter.write(msg);
			bufWriter.newLine();
			bufWriter.close();
			filerWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}

关于打印异常流(e.printStackTrace())到文件里,这里只简单说明一下,用业内可以去直接查看e.printstacktrace源代码,会发现是PrintStream打印只的,只用将
BufferedWriter改成PrintStream就行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值