android 日志工具类,Android日志工具类

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 + "\r\n").getBytes());

outputStream.write(msg.getBytes());

outputStream.write("\r\n".getBytes());

outputStream.flush();

} catch (IOException e) {

}

} else {

android.util.Log.i("SDCAEDTAG", "file is null");

}

}

break;

case RELEASE:

// 一般不做日志记录

break;

}

}

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值