-1

You may think it a stupid question, but surely I found no specification for it.

I have a mini-program that needs to log success and failures upon execution and I need the log to be kept apart from default ones so I can easily read it every time. Originally I put:

Logger.getLogger("Success").addHandler(new FileHandler("var/success.log", true));

My intention was to have a growing log and that every time I executed the program, the log would be growing. When I look at the log, it actually just has a lot of headers, once for each time I executed that line, even if no log was saved. I am pretty sure this isn't how it should work, I don't have 1 unique big log anymore but a lot of them in a single file, and switching to another formatter doesn't feel like a solution at all (i still need XML).

One solution I am currently searching for is to open a fresh file at every execution. The Pattern in the constructor doesn't seem to help with this. %u only test for already open or impossible to write, but with or without append enabled, an existing file won't stop this handler. And for %g, a rotating, it just rotates on size but the same I don't identify the previous log problem still happens.

is there a way to get there by modifying the Logger, LogManager, the constructor or the Filehandler? A my-own and specific solution using Files (or Path or File or whatever) to check for every combination before opening the logger doesn't seem efficient and its something I really don't really want to worry about.

Also, is there a way to get the FileHander to actually read the file input and CONTINUE with the log where it was left, instead of creating a new one with new headers?

This seconds question, although it fits my needs better, seems harder to get (but what would I know?), so I can work with the first one.

Thanks!

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
iaguitos
  • 1
  • 1

1 Answers1

0

Originally I put:

Logger.getLogger("Success").addHandler(new FileHandler("var/success.log", true));

Some things you'll want to improve in your JUL coding are:

  1. Hold on to the logger in a static final hard reference.
  2. Use an absolute path or a token that resolves to an absolute path for the filehandler.
  3. Use a static block so you don't create and add multiple file handlers to the logger.

My intention was to have a growing log and that every time I executed the program, the log would be growing. When I look at the log, it actually just has a lot of headers, once for each time I executed that line, even if no log was saved. I am pretty sure this isn't how it should work, I don't have 1 unique big log anymore but a lot of them in a single file, and switching to another formatter doesn't feel like a solution at all (i still need XML).

[snip]

Also, is there a way to get the FileHander to actually read the file input and CONTINUE with the log where it was left, instead of creating a new one with new headers?

That is how it works. The cause and solution are documented in this SO question. Here is a code example for CSV but would work the same for XML. Extend the XMLFormatter and override getHead and getTail.

One solution I am currently searching for is to open a fresh file at every execution. The Pattern in the constructor doesn't seem to help with this. %u only test for already open or impossible to write, but with or without append enabled, an existing file won't stop this handler. And for %g, a rotating, it just rotates on size but the same I don't identify the previous log problem still happens.

You are creating the FileHandler from code so you can simply code up logic to create a unique file name yourself.

new FileHandler("var/success" + System.nanoTime() + ".log", true);
Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47