import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; public class LOG { private static final int LEVEL_I = 1; private static final int LEVEL_D = 2; private static final int LEVEL_W = 3; private static final int LEVEL_E = 4; private static final String FILEPATH = "/folderPath"; private static final String FILENAME = "log.txt"; private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); public static void logD(String msg) {
log(LEVEL_D, msg);
} public static void logI(String msg) {
log(LEVEL_I, msg);
} public static void logW(String msg) {
log(LEVEL_W, msg);
} public static void logE(String msg) {
log(LEVEL_E, msg);
} public static void logE(Throwable ex) {
if (!AppConstants.DEBUG) {
return;
}
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
final Throwable t = AppConstants.DEBUG ? new Throwable() : null;
final StackTraceElement[] elements = t != null ? t.getStackTrace() : null;
String callerClassName = t != null ? elements[2].getClassName() : "N/A";
int pos = callerClassName.lastIndexOf('.');
if (pos >= 0) {
callerClassName = callerClassName.substring(pos + 1);
}
final String tag = callerClassName;
Log.e(tag, result);
try {
write(tag, result);
} catch (IOException e) {
e.printStackTrace();
}
} private static void log(int level, String msg) {
if (!AppConstants.DEBUG) {
return;
}
final Throwable t = AppConstants.DEBUG ? new Throwable() : null;
final StackTraceElement[] elements = t != null ? t.getStackTrace() : null;
String callerClassName = t != null ? elements[2].getClassName() : "N/A";
String callerMethodName = t != null ? elements[2].getMethodName() : "N/A";
int pos = callerClassName.lastIndexOf('.');
if (pos >= 0) {
callerClassName = callerClassName.substring(pos + 1);
}
final String tag = callerClassName;
final StringBuffer buf = new StringBuffer();
buf.append("[").append(callerMethodName).append("] ").append(msg); switch (level) {
case LEVEL_I:
Log.i(tag, buf.toString());
break;
case LEVEL_D:
Log.d(tag, buf.toString());
break;
case LEVEL_W:
Log.w(tag, buf.toString());
break;
case LEVEL_E:
Log.e(tag, buf.toString());
break;
default:
break;
}
try {
write(tag, buf.toString());
} catch (IOException e) {
e.printStackTrace();
}
} private static void write(String tag, String log) throws IOException {
String time = formatter.format(new Date(System.currentTimeMillis()));
String logger = time + ">>>>>" + tag + ">>>>>" + ">>>>>" + log + "\n\n";
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, FILEPATH);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(file + "/" + FILENAME, true);
fos.write(logger.getBytes());
fos.close();
}
}
}

Android sd卡log日志的更多相关文章

  1. Android SD卡创建文件和文件夹失败

    原文:Android SD卡创建文件和文件夹失败 功能需要,尝试在本地sd卡上创建文件和文件夹的时候,报错,程序崩溃. 一般情况下,是忘记给予sd卡的读写权限.但是这里面权限已经给了,还是报错. 在网 ...

  2. imx6 android SD卡启动

    工作中需要将imx6的android系统从SD卡启动,所以就分析了MfgTool中的脚本,分析android的分区情况,并尝试自己操作,竟然成功了,记录于此. 参考文档 http://www.kanc ...

  3. Android SD卡存储

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SDcard_store.html 一 概念 SD卡存储空间比较大,当需要存取较大的 ...

  4. Android SD卡读写

    package com.jredu.zuiyou.activity; import android.os.Bundle;import android.os.Environment;import and ...

  5. Android——SD卡工具类——SDCardUtils.java

    package com.xdsjs.save.utils; /** * SD卡相关的辅助类 * Created by xdsjs on 2015/10/13. */ import java.io.Fi ...

  6. Android SD卡上文件

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  7. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

  8. Android抓取log日志过滤

    前提:Android SDK已安装并配置环境变量 1.手机USB调试模式打开,连接PC 2.cmd窗口,执行adb logcat >log.log   // 输出日志到一个log文件 或者执行a ...

  9. Android Studio的Log日志调试

    本人菜鸟一枚,极大发挥了搜索的功能.现记录一番,以备后患. 用断点真的很烦,因为之前写linux的时候,就是用最蠢但是也是挺有帮助的printf()来进行调试. 其实用Log输出日志的原理也是差不多的 ...

随机推荐

  1. 使用DataSource绑定一维数组时,DataTextField只需绑定空字符串

    方法定义: public static void InitDropDownList(DropDownList ddl, bool isAddTopItem, DropDownList ddlSub, ...

  2. JSON.stringify报cyclic object value错误

    这是一个典型的循环引用的错误,一个对象里引用自己就会立刻得到这个错误: obj = { x:555, y: "hi" }; obj.myself = obj; try{ json ...

  3. 黏性Session和非黏性Session

    黏性Session和非黏性Session黏性Session:此模式下同一会话中的请求都被派送到同一个tomcat实例上,这样我们就无须在多台服务器之间实现session共享了,这是其好处,不好的地方就 ...

  4. ecshop安装

    ECSHOP开发中心(www.68ecshop.com) 1.下载ecshop程序包,下载地址: http://download.ecshop.com/2.7.3/ECShop_V2.7.3_UTF8 ...

  5. 代码记录——phase16,block36(修正后)

    加入边缘判定,<2则加2. if (x_upleft<2) x_upleft=x_upleft+2; if (y_upleft<2) y_upleft=y_upleft+2; HRE ...

  6. 【搞笑签名】390个qq个性昵称或签名,周末前娱乐一下

    1 来瓶82年的矿泉水 2 名不正则言承旭 3 天涯何处无芳草,还是母乳喂养好 4 她的妈妈不爱我 5 你丫的 6 农夫三拳 7 猪嚼在恋√痛 8 马驴脸猛鹿 9 小白兔兽性大发 10 曰捣一乱 11 ...

  7. Python多线程运行带多个参数的函数

    在python中经常会到用多线程处理某个函数来缩短运行时间. from multiprocessing import Pool def work(x): return x+1 pool = Pool( ...

  8. phpcms v9 添加内容出现500错误

    今天开发好一个网站,上传网站后,不知道安装的时候要求fsockopen()函数打开,我直接跳过安装完成的,所以搞得后面出错了,我一直以为这个问题,今天花了一天的时间找资料,测试,终于知道什么原因了,其 ...

  9. AES五种加密模式

    分组密码在加密时明文分组的长度是固定的,而实用中待加密消息的数据量是不定的,数据格式可能是多种多样的.为了能在各种应用场合安全地使用分组密码,通常对不同的使用目的运用不同的工作模式. 一.电码本模式( ...

  10. ZTree async中文乱码,ZTree reAsyncChildNodes中文乱码,zTree中文乱码

    ZTree async中文乱码,ZTree reAsyncChildNodes中文乱码,zTree中文乱码 >>>>>>>>>>>&g ...