Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle closing the java log file upon encountering an unhandled exception
    primarykey
    data
    text
    <p>I am currently working on a Java project where I am using the standard Java logger (i.e. java.util.logging.Logger). I'm finding that when I hit unexpected exceptions that are not handled in my code, my program closes but the logger doesn't release the lock on the text file.</p> <p>Is there some way to adjust my code so that <strong>any</strong> unexpected exception in my code gets logged to the output file, closes the output file and quits?</p> <p><strong>Some code in case it's relevant</strong></p> <p>I have a CustomLogging class:</p> <pre><code>import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; public class CustomLogging { static private FileHandler txtFileHandle; static private SimpleFormatter formatterTxt; static public void initalise() throws IOException { //Global logger configuration Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); logger.setLevel(Level.INFO); txtFileHandle = new FileHandler("Logging.txt"); formatterTxt = new SimpleFormatter(); txtFileHandle.setFormatter(formatterTxt); logger.addHandler(txtFileHandle); } static public void shutdown() throws IOException { txtFileHandle.close(); } } </code></pre> <p>The initialise method for the CustomLogger class is called at program start-up in the constructor of my first Java Swing GUI:</p> <pre><code>public StartMenu() { try { CustomLogging.initalise(); } catch (Exception e) { //Placeholder, will fix this up } //Other code.... } </code></pre> <p>All other classes get access via:</p> <pre><code>private final static Logger logger = Logger.getLogger(MyClassNameHere.class.getName()); </code></pre> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload