Android Log 输出到文件中去的简易方法

首先写一个处理 SDCard的java 方法

package com.share.app.util;

import android.os.Environment;
public class SDCardUtils {
   public static boolean isSdcardExist() {
return Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}

}


然后写一个处理Log的类

package com.share.app.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;
public class MyLogger {
private final static boolean logFlag = true;
private final static boolean logWriteToFile = true;
private final static String tag = "ShareApp";
private final static int logLevel = Log.VERBOSE;
private static Hashtable<String, MyLogger> sLoggerTable = new Hashtable<String, MyLogger>();
private String mClassName;
public static MyLogger getLogger(String className) {
MyLogger classLogger = (MyLogger) sLoggerTable.get(className);
if (classLogger == null) {
classLogger = new MyLogger(className);
sLoggerTable.put(className, classLogger);
}
return classLogger;
}
private MyLogger(String name) {
mClassName = name;
}
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 "[ " + Thread.currentThread().getName() + ": "
+ st.getFileName() + ":" + st.getLineNumber() + " ]";
}
return null;
}

public void i(Object str) {
if (logFlag) {
if (logLevel <= Log.INFO) {
String name = getFunctionName();
if (name != null) {
Log.i(tag, name + " - " + str);
if (logWriteToFile) {
writeLogToFile(name + " - " + str);
}
} else {
Log.i(tag, str.toString());
if (logWriteToFile) {
writeLogToFile(str.toString());
}
}
}
}


}


public void v(Object str) {
if (logFlag) {
if (logLevel <= Log.VERBOSE) {
String name = getFunctionName();
if (name != null) {
Log.v(tag, name + " - " + str);
Log.v(tag, "logWriteToFile" + " : " + logWriteToFile);
if (logWriteToFile) {
writeLogToFile(name + " - " + str);
}
} else {
Log.v(tag, str.toString());
if (logWriteToFile) {
writeLogToFile(str.toString());
}
}
}
}
}


public void w(Object str) {
if (logFlag) {
if (logLevel <= Log.WARN) {
String name = getFunctionName();
if (name != null) {
Log.w(tag, name + " - " + str);
if (logWriteToFile) {
writeLogToFile(name + " - " + str);
}
} else {
Log.w(tag, str.toString());
if (logWriteToFile) {
writeLogToFile(str.toString());
}
}
}
}
}


public void e(Object str) {
if (logFlag) {
if (logLevel <= Log.ERROR) {
String name = getFunctionName();
if (name != null) {
Log.e(tag, name + " - " + str);
if (logWriteToFile) {
writeLogToFile(name + " - " + str);
}
} else {
Log.e(tag, str.toString());
if (logWriteToFile) {
writeLogToFile(str.toString());
}
}
}
}
}


public void e(Exception ex) {
if (logFlag) {
if (logLevel <= Log.ERROR) {
Log.e(tag, "error", ex);
if (logWriteToFile) {
writeLogToFile(ex);
}
}
}
}


public void e(String log, Throwable tr) {
if (logFlag) {
String line = getFunctionName();
Log.e(tag, "{Thread:" + Thread.currentThread().getName() + "}"
+ "[" + mClassName + line + ":] " + log + "\n", tr);
if (logWriteToFile) {
writeLogToFile("{Thread:" + Thread.currentThread().getName()
+ "}" + "[" + mClassName + line + ":] " + log + "\n"
+ Log.getStackTraceString(tr));
}
}
}


public void d(Object str) {
if (logFlag) {
if (logLevel <= Log.DEBUG) {
String name = getFunctionName();
if (name != null) {
Log.d(tag, name + " - " + str);
Log.v(tag, "logWriteToFile" + " : " + logWriteToFile);
if (logWriteToFile) {
writeLogToFile(name + " - " + str);
}
} else {
Log.d(tag, str.toString());
if (logWriteToFile) {
writeLogToFile(str.toString());
}
}
}
}
}


@SuppressLint("SimpleDateFormat")
private void writeLogToFile(Object str) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd");
if (SDCardUtils.isSdcardExist()) {
File dir = Environment.getExternalStorageDirectory() ;
File file = new File(dir.getPath() 
+ File.separator + "360SafeApp" + File.separator + "log");
if (!file.exists()) {
file.mkdirs();
}


String fileName = file.getAbsolutePath() + File.separator + "log"
+ sf.format(new Date()) + ".txt";
File fil = new File(fileName);
if (!fil.exists()) {
try {
fil.createNewFile();
} catch (Exception e) {
}
}


FileOutputStream fos = null;
PrintWriter pw = null;
try {
fos = new FileOutputStream(fil, true);
pw = new PrintWriter(fos);
if (pw != null) {
pw.print(str + "\r\n");
pw.flush();
}
} catch (Exception e) {
} finally {
try {
if (fos != null) {
fos.close();
}
if (pw != null) {
pw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值