[color=red][size=xx-large] 知足常乐[/size][/color]
Log工具类:
Activity中调用:
即的权限的添加,ok !
Log工具类:
package com.zzl.test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.util.Log;
/**
* Created by zzl
* Date: 2012-03-03
* 记录log日志文件的工具类
*/
public class LogTool {
private static LogTool instance;
private PrintWriter logPrint;
private String logFile = "";
private String taskNo = "";//任务号
private String rootDir = "/sdcard/SGWS/log";//日记文件为sdcard/SGWS/任务号/日志文件
/**
* 配置没有头的log
*/
private LogTool(){
checkDate();//检查日期如果改变新建log文件
}
public static LogTool getInstance() {
if (instance == null) {
instance = new LogTool();
}
return instance;
}
/**
* 配置log文件名的头
*/
public LogTool(String logName){
checkDate();
}
/**
* 将java.util.Date格式转换为字符串格式”yyyy-MM-dd HH:mm:ss“
* 如Sat May 11 17:24:21 CST 2002 to '20002-05011 17:24:21'
* @param time Date日期
* @return字符串
*/
public String dateToString(Date time){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ctime = formatter.format(time);
return ctime;
}
/**
* 得到log文件名
*/
private String getLogFile(){
String date = "";
Calendar cd = Calendar.getInstance();
int y = cd.get(Calendar.YEAR);
int m = cd.get(Calendar.MONTH) + 1;
int d = cd.get(Calendar.DAY_OF_MONTH);
/* if(null==taskNo || "".equals(taskNo)){
date = rootDir+"log/" + logName + y + "-";
}else{
date = rootDir+taskNo+"/log/" + logName + y + "-";
}*/
date = rootDir+"/"+ y + "-";
if(m < 10)
date += 0;
date += m + "-";
if(d < 10)
date += 0;
date += d + ".log";
return date;
}
/**
* 配置log属性,如果没有新建log文件
*/
private void newLog(){
logFile = getLogFile();//创建log文件名
try{
File filePath = new File(rootDir);
if(!filePath.exists()){
filePath.mkdirs();
}
File file = new File(logFile);
if(!file.exists()){
file.createNewFile();
}
//(new File(logFile)).mkdir();
logPrint = new PrintWriter(new FileWriter(logFile, true), true);
}catch(IOException e){
File filePath = new File(rootDir);
if(!filePath.exists()){
filePath.mkdirs();
}
File file = new File(logFile);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try{
logPrint = new PrintWriter(new FileWriter(logFile, true), true);
}catch(IOException ex){
System.err.println("无法打开日志文件:" + logFile);
logPrint = new PrintWriter(System.err);
}
}
}
/**
* 检查日期如果改变新建log文件
*/
private void checkDate(){
Log.e("LogTool.checkDate()文件名:", logFile);
if(logFile == null || logFile.trim().equals("") || !logFile.equals(getLogFile())){
newLog();
}
}
/**
* 将文本信息写入日志文件,缺省为userdir/log
*/
public void log(String msg) {
//this.taskNo = taskNo;
checkDate();
logPrint.println(dateToString(new Date()) + "\t" + msg+"\r\n");
logPrint.close();
}
/**
* 将文本信息与异常写入日志文件
*/
public void log(Throwable e, String msg) {
//this.taskNo = taskNo;
checkDate();
logPrint.println(dateToString(new Date()) + "\t" + msg+"\r\n");
e.printStackTrace(logPrint);
logPrint.close();
}
}
Activity中调用:
package com.zzl.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
LogTool.getInstance().log("MainActivity.onCreate.btn.OnClickListener");
}
});
}
}
即的权限的添加,ok !