1) The default route can be found defined in the logging.properties file found in the \ jre \ lib directory of your Java installation, example:
C:\Program Files\Java\jdk1.8.0_102\jre\lib
inside shows the route:
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
2) To save the log in a specific path you can use this script that uses the class Logger in which you will define the name and path where the Log would be stored:
Logger logger = Logger.getLogger("LogdeNacho");
String pathLog = "C:/Datos/MyLog.log";
try {
FileHandler fhandler = new FileHandler(pathLog);
logger.addHandler(fhandler);
SimpleFormatter formatter = new SimpleFormatter();
fhandler.setFormatter(formatter);
} catch (SecurityException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
later you can add messages to your .log file using logger.info()
, for example:
logger.info("Probando el código!");
try {
String a = "SO";
int b = Integer.parseInt(a);
} catch (NumberFormatException ex) {
logger.info("ocurrió un error en el código!");
logger.info("NumberFormatException " + ex.getMessage());
}
logger.info("terminé...");
In your log file, this information would be saved:
ago 10, 2021 8:24:27 PM copydirectories.MainClass main
INFO: Probando el código!
ago 10, 2021 8:24:27 PM copydirectories.MainClass main
INFO: ocurrió un error en el código!
ago 10, 2021 8:24:27 PM copydirectories.MainClass main
INFO: NumberFormatException For input string: "SO"
ago 10, 2021 8:24:27 PM copydirectories.MainClass main
INFO: terminé...