我有一个Java记录器代码。它没有创建日志文件。虽然当我在另一个类中使用Logger.println时,它正在打印。但是我不确定它是否正在记录日志。
我已经编写了一个记录器类,并且正在另一个类中使用它来记录我的代码。
我的记录器类如下:
public class Logger{
private static String logFile=null;
public static boolean setLogFile(String newLogFile, Boolean b){
boolean done = false;
if (logFile ==null){
logFile = newLogFile;
File file = new File(logFile);
try{
done = file.createNewFile();
}
catch(IOException e){Logger.println("Error in creating log file" +logFile);
}
}
else {
File fromFile = new File(logFile);
File toFile = new File(newLogFile);
if(fromFile.exists()){
done = fromFile.renameTo(tofile);
if (done){
logFile = newLogFile;
}
else{
logger.println("can not move file");
}
}else{
Logger.println("file not exist";
}}
return done;
}
public static void print(String data){
system.out.println(data);
try{
FileWriter logger = new FileWriter (logFile, true);
logger.write(data);
logger.close();
}
catch(IOException e){System.out.println("can not write to file");
}}
logger.print保存到哪里? 日志保存在哪里?如何指定日志位置?
答案 0 :(得分:2)
您无需重新发明轮子。您可以轻松地集成其中一个可以在您方便的位置写入日志文件的记录器。
根据需要尝试使用Log4j,SL4j或可用的依赖项。
这些现有的实现为您提供细粒度的日志记录。您可以打印信息,错误,调试,配置和警告。集成起来比较容易,开发人员也可以专注于业务逻辑。