Android日志工具类
发布时间:2020-05-24 14:51:40 所属栏目:Java 来源:互联网
导读:Android日志工具类
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import org.apache.commons.lang3.time.DateFormatUtils;
import android.os.Environment;
import android.util.Log;
/**
* 日志记录
*
*/
public class LogUtil {
/**
* 开发阶段
*/
private static final int DEVELOP = 0;
/**
* 内部测试阶段
*/
private static final int DEBUG = 1;
/**
* 公开测试
*/
private static final int BATE = 2;
/**
* 正式版
*/
private static final int RELEASE = 3;
/**
* 当前阶段标示
*/
private static int currentStage = DEVELOP;
private static String path;
private static File file;
private static FileOutputStream outputStream;
private static String pattern = "yyyy-MM-dd HH:mm:ss";
static {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File externalStorageDirectory = Environment.getExternalStorageDirectory();
path = externalStorageDirectory.getAbsolutePath() + "/zcw/";
File directory = new File(path);
if (!directory.exists()) {
directory.mkdirs();
}
file = new File(new File(path),"log.txt");
android.util.Log.i("SDCAEDTAG",path);
try {
outputStream = new FileOutputStream(file,true);
} catch (FileNotFoundException e) {
}
} else {
}
}
public static void info(String msg) {
info(LogUtil.class,msg);
}
public static void info(Class clazz,String msg) {
switch (currentStage) {
case DEVELOP:
// 控制台输出
Log.i(clazz.getSimpleName(),msg);
break;
case DEBUG:
// 在应用下面创建目录存放日志
break;
case BATE:
// 写日志到sdcard
Date date = new Date();
String time = DateFormatUtils.format(date,pattern);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if (outputStream != null) {
try {
outputStream.write(time.getBytes());
String className = "";
if (clazz != null) {
className = clazz.getSimpleName();
}
outputStream.write((" " + className + "rn").getBytes());
outputStream.write(msg.getBytes());
outputStream.write("rn".getBytes());
outputStream.flush();
} catch (IOException e) {
}
} else {
android.util.Log.i("SDCAEDTAG","file is null");
}
}
break;
case RELEASE:
// 一般不做日志记录
break;
}
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
