I'd like to have 1 configuration file that contains information about how/where/what logger in particular class should log.
Example:
class Foo
package myApp;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
class Foo {
static final Logger logger = Logger.getLogger(Foo.class.getName());
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\\path\\to\\myApp.log.properties");
LogManager.getLogManager(). readConfiguration(fis);
logger.log(Level.INFO, "Msg message");
Bar obj = new Bar();
obj.doSth();
}
class Bar
package myApp;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Bar {
static final Logger logger = Logger.getLogger(Bar.class.getName());
public Bar() {
}
public void doSth() {
logger.log(Level.WARNING, "Msg message");
}
}
File myApp.log.properties
handlers =
config =
myApp.Foo.handlers = java.util.logging.ConsoleHandler
myApp.Foo.useParentHandlers = false
myApp.Foo.level = INFO
myApp.Bar.handlers = java.util.logging.ConsoleHandler
myApp.Bar.useParentHandlers = false
myApp.Bar.level = INFO
Output:
Jul 23, 2014 1:27:12 PM myApp.Bar doSth
WARNING: Msg message
As you can see, the log record for Foo is missing, i guess it's because logger is static and is created before configuration for LogManager is loaded and set.
Could you please suggest solution where log record is printed for Foo's logger? Also with a static logger and without using commandline parameter -D configFile
when the program is executed?