0

I wrote a CXF SOAP client and would like to create numbered log files for each run.

I have tried configuring FileHandler using "logging.properties" file. I have this line there:

handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = campaignStatusCXF%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# Limit the message that are printed on the console to WARNING and above.
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

But it does not work just by itself. I had to add FileHandler in the code:

System.setProperty("java.util.logging.config.file", "/logging.properties"); LOG = Logger.getLogger(BulkEmailDownloader.class.getName());

try {
    FileHandler logFile;
    logFile = new FileHandler("campaignStatus.log");
    logFile.setFormatter(new SimpleFormatter());
    LOG.addHandler(logFile);
} catch (SecurityException e1) {            
} catch (IOException e2) {          
} 

The problem with this approach is - it simply rewrites the log all the time.

I read numerous examples and answers, for example this, but could not find the answer I am looking for.

Community
  • 1
  • 1
Daniil Shevelev
  • 11,739
  • 12
  • 50
  • 73

1 Answers1

0

The problem is that the the LogManager configuration runs before your code is executed. This means you have to do one of the following:

  1. Set the 'java.util.logging.config.file' system property in your startup script.
  2. Call LogManager.readConfiguration() after you set the system property in your code.
  3. Use ClassLoader.getResourceAsStream(String) and LogManager.readConfiguration(InputStream) to load your properties file.

Also, your example code needs to include a file pattern:

new FileHandler("campaignStatus%g.log");
jmehrens
  • 10,580
  • 1
  • 38
  • 47