android 日志工具类,分享一个Android日志记录的工具类

package cn.mucang.android.weizhanglib.data;

import android.os.Environment;

import android.text.format.DateFormat;

import android.util.Log;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.io.StringWriter;

/**

* Created by sanders on 14-10-8.

*/

public class Loger {

private static String TAG = "LOGER";

private static File logFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/XiaoMiSiJi.log");

private static String fmt = "[%s] ";

private static String df = "yyyy-MM-dd kk:mm:ss";

private static FileWriter fw = null;

private static BufferedWriter bw = null;

private static StringWriter sw = null;

private static PrintWriter pw = null;

private static boolean log = true;

private static boolean write = true;

static {

try {

if (write) {

if (!logFile.exists()) {

logFile.createNewFile();

}

if (fw == null) {

fw = new FileWriter(logFile, true);

}

if (bw == null) {

bw = new BufferedWriter(fw);

}

}

} catch (Exception e) {

Log.e(TAG, "初始化日志组建失败 ", e);

write = false;

}

}

public static void write(String logs) {

String logString = String.format(fmt, DateFormat.format(df, System.currentTimeMillis()).toString()) + logs;

try {

bw.write(logString);

bw.newLine();

bw.flush();

fw.flush();

} catch (Exception e) {

Log.e(TAG, "write() ", e);

write = false;

}

}

public static void write(String log, Throwable throwable) {

sw = new StringWriter();

pw = new PrintWriter(sw);

throwable.printStackTrace(pw);

String error = sw.getBuffer().toString();

String logString = String.format(fmt, DateFormat.format(df, System.currentTimeMillis()).toString()) + log + "==>" + error;

try {

bw.write(logString);

bw.newLine();

bw.flush();

fw.flush();

} catch (Exception e) {

Log.e(TAG, "write() error", e);

write = false;

} finally {

try {

sw.close();

pw.close();

} catch (Exception e) {

Log.e(TAG, "write() error", e);

write = false;

}

sw = null;

pw = null;

}

}

public static void i(String msg) {

i(TAG, msg);

}

public static void i(String tag, String msg) {

if (log) {

Log.i(tag, msg);

}

if (write) {

write(msg);

}

}

public static void v(String msg) {

v(TAG, msg);

}

public static void v(String tag, String msg) {

if (log) {

Log.v(tag, msg);

}

if (write) {

write(msg);

}

}

public static void d(String msg) {

d(TAG, msg);

}

public static void d(String tag, String msg) {

if (log) {

Log.d(tag, msg);

}

if (write) {

write(msg);

}

}

public static void w(String msg) {

w(TAG, msg);

}

public static void w(String tag, String msg) {

if (log) {

Log.w(tag, msg);

}

if (write) {

write(msg);

}

}

public static void e(String msg) {

e(TAG, msg);

}

public static void e(String tag, String msg) {

if (log) {

Log.e(tag, msg);

}

if (write) {

write(msg);

}

}

public static void e(String msg, Throwable e) {

e(TAG, msg, e);

}

public static void e(String tag, String msg, Throwable e) {

if (log) {

Log.e(tag, msg, e);

}

if (write) {

write(msg, e);

}

}

//这个释放资源。若退出程序部在记录和打印日志则调用

public static void close() {

try {

if (fw != null) {

fw.close();

fw = null;

}

if (bw != null) {

bw.close();

bw = null;

}

if (sw != null) {

sw.close();

sw = null;

}

if (pw != null) {

pw.close();

pw = null;

}

} catch (Exception e) {

Log.e(TAG, "close() error  ", e);

write = false;

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值