android log 方法,Android log 方法

packagetest;importjava.io.File;importjava.io.FileOutputStream;importjava.io.PrintStream;importjava.util.Date;importandroid.os.Environment;public class Log extendsLogger {private static final String APP_TAG = "renwuto";private static final String LOG_FILE_NAME = "renwuto.txt";private staticPrintStream logStream;private static final String LOG_ENTRY_FORMAT = "[%tF %tT]%s";publicLog(String name) {super(name);

}

@Overrideprotected voiddebug(String str) {

android.util.Log.d(APP_TAG, str);

write(str,null);

}

@Overrideprotected voiderror(String str) {

android.util.Log.e(APP_TAG, str);

write(str,null);

}

@Overrideprotected voidinfo(String str) {

android.util.Log.i(APP_TAG, str);

write(str,null);

}

@Overrideprotected voidwarn(String str) {

android.util.Log.w(APP_TAG, str);

write(str,null);

}

@Overrideprotected voiddebug(String str, Throwable tr) {

android.util.Log.d(APP_TAG, str);

write(str, tr);

}

@Overrideprotected voiderror(String str, Throwable tr) {

android.util.Log.e(APP_TAG, str);

write(str, tr);

}

@Overrideprotected voidinfo(String str, Throwable tr) {

android.util.Log.i(APP_TAG, str);

write(str, tr);

}

@Overrideprotected voidwarn(String str, Throwable tr) {

android.util.Log.w(APP_TAG, str);

write(str, tr);

}private voidwrite(String msg, Throwable tr) {if (!Log.DBG) {return;

}try{if (null ==logStream) {synchronized (Log.class) {if (null ==logStream) {

init();

}

}

}

Date now= newDate();if (null !=logStream) {

logStream.printf(LOG_ENTRY_FORMAT, now, now, msg);

logStream.print("\n");

}if (null !=tr) {

tr.printStackTrace(logStream);if (null !=logStream) {

logStream.print("\n");

}

}

}catch(Throwable t) {//Empty catch block

}

}public static voidinit() {if (!Log.DBG) {return;

}try{

File sdRoot= null;

String state=Environment.getExternalStorageState();if(Environment.MEDIA_MOUNTED.equals(state)) {

sdRoot=Environment.getExternalStorageDirectory();

}if (sdRoot != null) {

File logFile= newFile(sdRoot, LOG_FILE_NAME);

android.util.Log.d(APP_TAG,"Log to file : " +logFile);

logStream= new PrintStream(new FileOutputStream(logFile, true), true);

}

}catch(Throwable e) {//Empty catch block

}

}

@Overrideprotected void finalize() throwsThrowable {try{super.finalize();if (logStream != null) {

logStream.close();

}

}catch(Throwable t) {//Empty catch block

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值