0

Using Java.util.logging's FileHandler class to create cyclic logs. However why these logs are appended with .0 extension. .1,.2,.3 etc are fine, I only do not need .0 as my extension of file, since its confusing for the customer. Any way to achieve the same? I am using java version java version "1.8.0_144".

FileHandler fileTxt = new FileHandler(pathOfLogFile+"_%g.log",
                Integer.parseInt(prop.getProperty("MAX_LOG_FILE_SIZE")),
                Integer.parseInt(prop.getProperty("NO_OF_LOG_FILES")), true);
SimpleFormatter formatterTxt = new SimpleFormatter();
        fileTxt.setFormatter(formatterTxt);
        logger.addHandler(fileTxt);

Name of log file is LOG_0.log. requirement is to not to have _0 on the latest file, need to be simply LOG.log.

Mahesh
  • 29
  • 8
  • Bad luck, you'll either have to write your own handler or let your client remain confused, if that's really the case. Hard to believe this is really worth worrying about. – user207421 Oct 27 '17 at 03:27
  • Very true. I think they were the log4J user earlier, so that is why they need this flexibility. I will try one more day to figure something out, else they have to digest the 0 with the log file :) for now. – Mahesh Oct 27 '17 at 06:37

2 Answers2

2

You'll have to add more information about how your are setting up your FileHandler. Include code and or logging.properties file.

Most likely you are creating multiple open file handlers and are simply not closing the previous one before you create the next one. This can happen due to bug in your code or that you are simply not holding a strong reference to the logger that holds your FileHandler. Another way to create this issue is by create two running JVM processes. In which case you have no option but to choose the location of the where the unique number is placed in your file name.

Specify the %g token and %u in your file pattern. For example, %gfoo%u.log.

Per the FileHandler documentation:

If no "%g" field has been specified and the file count is greater than one, then the generation number will be added to the end of the generated filename, after a dot.

[snip]

Normally the "%u" unique field is set to 0. However, if the FileHandler tries to open the filename and finds the file is currently in use by another process it will increment the unique number field and try again. This will be repeated until FileHandler finds a file name that is not currently in use. If there is a conflict and no "%u" field has been specified, it will be added at the end of the filename after a dot. (This will be after any automatically added generation number.)

Thus if three processes were all trying to log to fred%u.%g.txt then they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as the first file in their rotating sequences.

If you want to remove the zero from the first file only then the best approximation of the out of the box behavior would be to modify your code to:

  1. If no LOG_0.log file exists then use File.rename to add the zero to the file. This changes LOG.log -> LOG_0.log.
  2. Trigger a rotation. Results in LOG_0.log -> LOG_1.log etc. Then LOG_N.log -> LOG_0.log
  3. Use File.rename to remove the zero from the file. LOG_0.log -> LOG.log
  4. Open your file handler with number of logs as one and no append. This wipes the oldest log file and starts your new current one.

The problem with this is that your code won't rotate based on file size during a single run.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Thanks for reply. Edited the question and added code snippet too. I only do not want to 0 in my latest log file created. The configure method of FileHandler is private and when tried to override the implementations, many other dependencies came up. Please suggest, in case there is a better way – Mahesh Oct 27 '17 at 02:59
  • @Mahesh I see. Updated the answer but you'll probably have to follow the advice of `EJP`. – jmehrens Oct 27 '17 at 13:54
0

Simply use logger name as filename (Don't include %g in it). The latest file will be filename.log. Also note that the rotated files will have the numbers as extension.

Aravindharaj G
  • 407
  • 5
  • 16