package com.hitachi.hscms.util.tools;
/**
* This file is part of HSCMS project which started by ossl lab of Hitachi(China) Research and Development Corp.
* HSCMS dedicate to give a whole configuration environment for xp developing process,including auto integration etc. functions.
* This class is a tool to read and write log file
* @author Li linlin
* @date 2006-08-10
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.hitachi.hscms.Messages;
public class LogFileOperator {
private static Logger logger = Logger.getLogger(LogFileOperator.class);
public static final String logFilePath = Messages.getInstance().getString("LogFileRoot");
/**
* Fuction: To read log file of a pointed task
* @Param: String , task name
* @return: String, file content
* @throws Exception: has catched IOException and FileNotFoundException
* @throws SQLException: null
*/
public static String readTaskLog(String taskName)
{
String result="";
logger.info("start read");
File file = new File(logFilePath+File.separator+taskName+".log");
logger.info(file.getAbsolutePath());
if(file.exists())
{
logger.info("file exists");
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String tmp;
try {
while((tmp =br.readLine())!=null)
{
logger.info("has read ");
result += (tmp+System.getProperty("line.separator"));
}
br.close();
fr.close();
} catch (IOException e) {
logger.error("read file "+file.getAbsolutePath()+" error");
e.printStackTrace();
}
} catch (FileNotFoundException e) {
logger.error("File " + file.getAbsolutePath()+" not found");
e.printStackTrace();
}
}
return result;
}
/**
* Fuction: To write infomation of a pointed task into log file ,each task has a log file name named by taskname
* and each infomation will be added as a line
* @Param: String , task name ;String, the information to write to the log file
* @return: boolean, if success,return true
* @throws Exception: has catched IOException and FileNotFoundException
* @throws SQLException: null
*/
public static boolean writeTaskLog(String info,String taskName)
{
File file = new File(logFilePath);
if(!file.exists())
file.mkdir();
logger.info("Start write");
file = new File(logFilePath+File.separator+taskName+".log");
try {
FileWriter fw= new FileWriter(file,true);
BufferedWriter br = new BufferedWriter(fw);
br.write(info);
logger.info("Write string "+info + "into file" +file.getAbsolutePath());
br.newLine();
br.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
logger.info("End write");
return true;
}
/**
* Fuction: Delete all the logs when the system restart
* @Param: null
* @return: null
* @throws Exception: null
* @throws SQLException: null
*/
public static void clearLogs()
{
File file = new File(logFilePath);
File[] logs = file.listFiles();
if(logs!=null && logs.length>0)
{
for(int i=0;i<logs.length;i++)
{
logs[i].delete();
}
}
}
/**
* Fuction: Delete the log of the task when the system restart
* @Param: null
* @return: null
* @throws Exception: null
* @throws SQLException: null
*/
public static void clearLogs(String taskName)
{
File file = new File(logFilePath+File.separator+taskName+".log");
if(file.exists())
file.delete();
}
public static boolean editTaskLog(String oldName,String newName)
{
File file = new File(logFilePath+File.separator+oldName+".log");
if(file.exists()){
return file.renameTo(new File((logFilePath+File.separator+newName+".log")));
}
return false;
}
public static boolean deleteTaskLog(String name)
{
File file = new File(logFilePath+File.separator+name+".log");
if(file.exists()){
return file.delete();
}
return false;
}
}