0

I have a persistent event-driven HTTP upload program (hot folder) that writes to a log on every action. It runs indefinitely until the user decides to close it.

To ensure the log is closed when the application is closed, I have written the program so on every write to log, it opens the log file, writes to it, and then closes the log.

Like so:

fh = new FileHandler(logName, true);
fh.setFormatter(new MyCustomFormatter());
logger.addHandler(fh);
logger.info(message);
logger.removeHandler(fh);
fh.close();

Recently, I have considered reducing the number of open/closes by opening the log for the duration of an upload job (potentially hundreds of writes to the log) and then closing when the job is done.

Long story short, how much of a performance gain would I expect from this? Are there other options to ensuring the application closes without an unclosed log file?

jahmezz
  • 416
  • 1
  • 5
  • 15
  • 1
    seems like it should be pretty easy to test this... – jtahlborn Aug 13 '14 at 18:21
  • 1
    assuming you are using the java.util.log facilities, the jdk already has builtin support to close these out at jvm shutdown. – jtahlborn Aug 13 '14 at 18:27
  • what is this built-in support? as in the file should automatically close if i close the application? that hasn't been my experience. i have had to call fh.close() to make sure the .lck file is gone when I close the program. You're right that I can test it though. Just wanted an opinion on it. Thanks @jtahlborn – jahmezz Aug 13 '14 at 18:43
  • Never mind, I understand what you are saying. Shutdown hooks. I had it this way, but it only runs on expected shutdowns. What if the user force-closes the program? @jtahlborn – jahmezz Aug 13 '14 at 18:47
  • 1
    if the user force closes the program, then you lose any buffered output (the OS will still close the file for you). as with most things programming, you have to balance your performance needs with your reliability needs. – jtahlborn Aug 13 '14 at 18:58

1 Answers1

1

By closing the FileHandler you might be paying the cost of a sync.

If you leave your FileHandler attached to a logger at the time the JVM is shutdown the LogManager will close the log file. If you remove the handler from the logger you are responsible for closing it. If your logger is garbage collected before shutdown then the FileHandler is not closed. This type of 'file litter' can be corrected through changes to your code.

If your JVM crashes or there is an I/O exception related to closing of the log/lck file then you will see lck files left over and log files that may not contain your formatter tail string. That case is rare but, there is no way to prevent it. You have to apply a detect and correct strategy to get rid of these cases.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47