I am trying to log the program's execution flow to better understand how servlets, EJBs and JSPs work together. Currently the difficulty I am facing is to output the log to a local file.
I have first tried with the Java logger API, studying this example:
Using Java log API:
And I have written:
package beans;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
@Stateless
public class ComentarioNota {
private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());
public String convierteComentarioNota(String evaluacion, String comentario) {
log.logp(Level.WARNING,
this.getClass().getName(),
this.getClass().getName(), this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);
if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
return "Apto";
} else {
return "No Apto";
}
}
And it indeed outputs the log with ClassName::ClassMethod::User input info.
However it outputs the log info to the console, I need it in a local file, how could we log into a local file?
I have tried to use the PrintWriter and creating a new file with its contents:
package beans;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
@Stateless
public class ComentarioNota {
private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());
public String convierteComentarioNota(String evaluacion, String comentario) {
try (PrintWriter out = new PrintWriter("log.txt")) {
out.println(this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);
if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
return "Apto";
} else {
return "No Apto";
}
} catch (FileNotFoundException ex) {
Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
}
return "No Apto";
}
}
However this does not create a new file.
How could we create that new local file and send the log output to it?
Thank you for your help!.
I have also read: How do I save a String to a text file using Java?
Where does the ServletContext.log messages go in tomcat 7?
EDIT: I have read the comment's tutorial: http://tutorials.jenkov.com/java-logging/handlers.html#streamhandler
ANd I have found that we can add handlers to the logger, and there is a built in handler which is FileHandler which is supposed to create one file with the log's contents. I have tried the following:
package beans;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
@Stateless
public class ComentarioNota {
private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());
public String convierteComentarioNota(String evaluacion, String comentario) {
try {
FileHandler handler = new FileHandler("comentarioNota.txt");
log.addHandler(handler);
log.logp(Level.WARNING,
this.getClass().getName(),
"convierteComentarioNota", this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);
if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
return "Apto";
} else {
return "No Apto";
}
} catch (IOException ex) {
Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
}
return "No Apto";
}
}
However I still seeing the log being outputted to the console and no file is being created:
And no file is being creted because at least it does not show up in the IDE and I have also try to find it:
Could you help me figuring out how to log to a local file properly?
Thank you for your help.