在做测试的时候遇到总是,开发人员总想在手机打印一些结果好查看一下总是原因或用于了解一些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就行了