Question

I have 2 configuration files for logging, config1.properties and config2.properties

When I load the config1.properties and log something, the format is correct, but right after, when I load the second config file, the changes are not reflected. Here is my code:

System.setProperty("java.util.logging.config.file", "config1.properties");
logger = Logger.getLogger(this.getClass().getSimpleName());
logger.info("Message 1");
System.setProperty("java.util.logging.config.file", "config2.properties");
LogManager logManager = LogManager.getLogManager();
logManager.readConfiguration();
logger = Logger.getLogger("NewLogger");
logger.info("Message 2");

I have set the configuration in config2.properties to log messages in 2 lines, however the message is still showing in one line.

Any ideas why the new configuration is not taking effect? I am sure that my config files are correct, because I tried loading config2 before config1, and that showed my logged messages in 2 lines.

Here is the logged result:

[01-13-2014 16:48:56:186] LoggerUnitTest INFO: Message 1
[01-13-2014 16:48:56:195] LoggerUnitTest INFO: Message 2

It should show up as :

[01-13-2014 16:48:56:186] LoggerUnitTest INFO: Message 1
[01-13-2014 16:48:56:195] LoggerUnitTest INFO: 

Message 2

Below are the config files I am using:

config1.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.formatter.format = [%1$tm-%1$td-%1$tY %1$tk:%1$tM:%1$tS:%1$tL] %4$s: %5$s%6$s%n

config2.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Note that this line is different from the line in config1
java.util.logging.ConsoleHandler.formatter.format = [%1$tm-%1$td-%1$tY %1$tk:%1$tM:%1$tS:%1$tL] %n %4$s: %5$s%6$s%n
Was it helpful?

Solution

This works for me:

Test.java

import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Test {
  public static void main(String[] args) throws Exception {
    System.setProperty("java.util.logging.config.file", "config1.properties");
    Logger logger = Logger.getLogger(Test.class.getSimpleName());
    logger.info("Message 1");
    System.setProperty("java.util.logging.config.file", "config2.properties");
    LogManager logManager = LogManager.getLogManager();
    logManager.readConfiguration();
    logger = Logger.getLogger(Test.class.getSimpleName());
    logger.info("Message 2");
  }
}

config1.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

config2.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter

javac Test.java
java Test

Jan 13, 2014 8:51:20 PM Test main
INFO: Message 1
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2014-01-13T20:51:20</date>
  <millis>1389664280170</millis>
  <sequence>1</sequence>
  <logger>Test</logger>
  <level>INFO</level>
  <class>Test</class>
  <method>main</method>
  <thread>10</thread>
  <message>Message 2</message>
</record>

OTHER TIPS

Look at the Documentation of the Logger.getLogger(String name).documentation

it says

If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's handlers. It will be registered in the LogManager global namespace.

So Even though set a new configuration properties your logger instance have the old configuration

try getting a new instance by calling following line again

logger = Logger.getLogger("new Name");

may be you might have to change the input parameter name differently. or it will return the old logger object

EDIT

Here the sample code i tried

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class LoggingTest {

    public static void main(String[] args) {
        System.setProperty("java.util.logging.config.file", "config1.properties");
        Logger logger = Logger.getLogger(LoggingTest.class.getSimpleName());
        logger.info("Message 1");
        System.setProperty("java.util.logging.config.file", "config2.properties");
        LogManager logManager = LogManager.getLogManager();
        try {
            logManager.readConfiguration();//logManager.readConfiguration(new FileInputStream(new File("config2.properties")));
        } catch (IOException ex) {
            Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
        }

        logger = Logger.getLogger("NewLogger");
        logger.info("Message 2");

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top