Reputation: 45
I have a logger code in Java. It is not creating a log file. Though when I am using Logger.println in another class it is printing. But I am not sure if it is logging or not.
I have written a logger class and I am using this in another class to log my code.
My logger class looks like below:
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");
}}
where is logger.print saving to? where are the logs being saved or how can i specify the location of my logs?
Upvotes: 0
Views: 140
Reputation: 41
You no need to reinvent the wheel again. You can easily integrate one of the loggers that can write a log file at your convenient location.
Try Log4j, SL4j or available dependencies based on your need.
These existing implementations give you fine-grained loggings. You can print info, error, debug, config and warnings. It would be easier to integrate as well as developer can concentrate on business logic.
Upvotes: 2