我必须创建一个Java服务,使用Logger库将某些数据记录到文件中。每天的日志数据包含在单个文件中,该文件是通过将日期附加到基本文件名生成的。以下是上述
的Servlet代码........
import java.util.logging.Logger;
.........
public class track extends HttpServlet {
private String BaseDirectory;
private String BaseFileName;
private String FullFileName;
private Logger logger;
private FileHandler fh;
private ServletContext sc;
@Override
public void init(){
ServletConfig config=getServletConfig();
BaseDirectory=config.getInitParameter("LogBaseDir");
BaseFileName=config.getInitParameter("BaseLogFile");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
sc=getServletContext();
FullFileName=getFullFileName();
String realPath = sc.getRealPath("/"+BaseDirectory);
String FullFilePath=realPath+"/"+FullFileName;
createFile(realPath,FullFilePath);
String qs=request.getQueryString();
if(qs!=null && qs.contains("paramjson")){
logData(request.getQueryString(),FullFilePath);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
private String getFullFileName() {
Date date=new Date();
SimpleDateFormat df=new SimpleDateFormat("dd-MM-yyyy");
String FullName=this.BaseFileName+"_"+df.format(date)+".log";
return FullName;
}
private void createFile(String realPath, String FullFilePath) throws IOException {
/* Creates the base logging Directory denoted by realPath.
Checkes whether the log file denoted by FullFilePath exists or not. If it doesn't,
it is created.
*/
boolean status1,status2;
File file=new File(realPath);
synchronized(this){
status1=file.mkdirs();
}
if (status1){
sc.log("Directory Created");
}
else{
sc.log("Directory already exists");
}
file=new File(FullFilePath);
synchronized(this){
file=new File(FullFilePath);
status2=file.exists();
if(status2){
sc.log("File already exists with name "+FullFileName);
}
else{
if(file.createNewFile()){
sc.log("File Creation Successfull with name "+FullFileName);
}
else{
sc.log("File Creation Failed with name "+FullFileName);
}
}
}
}
private void logData(String queryString, String FullFilePath) throws IOException {
logger=Logger.getLogger("ClickLogger");
if(fh==null){
fh=new FileHandler(FullFilePath);
logger.addHandler(fh);
}
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
String qs = decodeData(queryString);
logger.log(Level.INFO,qs);
}
private String decodeData(String queryString) throws UnsupportedEncodingException {
return (java.net.URLDecoder.decode(queryString,"UTF-8"));
}
}
这些功能是不言自明的。执行的一般顺序通常根据需要进行。但是,如果出现以下两种情况:
a)代码已修改
b)生成的日志文件是有意删除的(手动)
原始日志文件附加一个数字(例如Data.log.1 Data.log.2等)。如上所述,数据被正确记录到多个文件中。因此,为了让事情回到正轨,我必须删除基本日志目录,重新启动tomcat然后继续。
删除日志文件的第二种情况并不像在很少发生的生产环境中那样令人担忧。但是,当我修改我的代码并部署war文件时,我不想每次都重新启动我的服务器。
在处理此问题时,我修改了logData
中的以下行,如下所示:
if(fh==null){
fh=new FileHandler(FullFilePath);
logger.addHandler(fh);
}
到
if(fh==null){
fh=new FileHandler(FullFilePath,true);
logger.addHandler(fh);
}
这解决了创建新文件(数字附加的文件)的问题。但是,现在没有更多数据记录到文件中,它仍然为空。我在这做错了什么?请帮忙
答案 0 :(得分:1)