I would like to record the logs of my Java application. I have created this class:
public class Log {
final Logger logger = Logger.getLogger("DigiScope.log");
public static void main(String[] args) {
}
public Log(String message) {
try {
// Create an appending file handler
boolean append = true;
FileHandler handler = new FileHandler("my.log", append);
// Add to the desired logger
Logger logger = Logger.getLogger("com.mycompany");
logger.addHandler(handler);
logger.info(message);
} catch (IOException e) {
}
}
}
And for each button I have a code like that:
private void btnNewPatient ActionPerformed(java.awt.event.ActionEvent evt) {
Log a = new Log("New Patient created");
}
This code creates a log.txt, but records only the click on the first button, the others clicks on the others buttons are not record.
Can you help me?
Thank you.
java.util.logging? Anyway, you should not create and add a handler to the logger each time a button is clicked. You should add the handler once (either by configuration or by code), and have each click log a message using the logger.static finalmember fieldloggerin your class, calllogger.infofrom the action listener, and have a logging.properties which configure the logged messages to go to the file. Is there a good reason not to adhere to this in your case?