Where are the Java Logs stored? How can I choose where?

1

I imagine that there must be a default route where the Logs are saved when using the Java Logger. My questions are two:

  

1) What is that default route?

     

2) How do I specify a different one?

I am not using any external library (log4j), only Java 1.8.

import java.util.logging.Level;
import java.util.logging.Logger;
    
asked by Nacho Zve De La Torre 08.08.2018 в 22:56
source

1 answer

2

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é...
    
answered by 08.08.2018 / 23:20
source