Depends on the LogManager that you are using. Assuming this is the standard LogManager you can change all ConsoleHandler instances using:
com.example.foo.level=INFO
com.example.foo.handlers=java.util.logging.FileHandler,java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = SEVERE
java.util.logging.FileHandler.level = INFO
If you want to tweak only one instance of a ConsoleHandler then it is more complicated as it always involves shipping some logging code with your project. Here are the options to choose from:
- You can create a subclass of ConsoleHandler as a public class with a public no-arg constructor. This new fully qualified class name provides a unique namespace for the LogManager properties file. You then install that subclass to the logger you want to tweak instead of the ConsoleHandler. More unique handlers you need the more subclasses you create. For example:
package com.example;
public ConsoleHandler1 extends ConsoleHander {
}
Then in your properties file looks like:
com.example.foo.level=INFO
com.example.foo.handlers=java.util.logging.FileHandler,com.example.ConsoleHandler1
java.util.logging.ConsoleHandler.level = INFO
com.example.ConsoleHandler1.level = SEVERE
java.util.logging.FileHandler.level = INFO
- Use the LogManager
config
option. Per the docs:
A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.
Config classes are very free form and you can even read your own custom LogManager properties. An example class would be:
package com.example;
import java.util.logging.*;
public class FooLoggerConsoleSevere {
public static final Logger logger = Logger.getLogger("com.example.foo");
public FooLoggerConsoleSevere() {
ConsoleHandler con = new ConsoleHandler();
con.setLevel(Level.SEVERE);
logger.addHandler(con);
}
}
Then run the configuration like so:
config=com.example.FooLoggerConsoleSevere
com.example.foo.level=INFO
java.util.logging.FileHandler.level = INFO
- You can always write a method in your application to locate the logger and locate the handler and change the level.