- Java Logging: Configuration
- Configuration Class
- Configuration File
- Java Util Logging — Loading logging.properties
- Loading logging.properties from absolute path
- Output
- Loading logging.properties from classpath
- src/main/resources/logging.properties
- By getting absolute location of the classpath resource
- Output
- By using LogManager#readConfiguration()
- Output
- Example Project
Java Logging: Configuration
The initialization of the configuration is taken care of by the java.util.logging.LogManager class.
Configuration Class
You can use a Java class to configure the Java Logging API. You do so by specifying the name of the class in the JVM parameter java.util.logging.config.class . It is the constructor of that class that should load the configuration and apply it to the Logger ‘s in the hierarchy.
Configuration File
If no configuration class is specified, you can instead specify a configuration file (but no configuration class can be specified then!).
The Java Logging API has a default logging configuration file located at «lib/logging.properties» , inside the JRE directory. If you edit this file, you edit the default logging settings for the entire JRE, for every program executed. This is most often not what you want to do, though.
Instead, you can set a separate configuration file for your application. You do so by setting the JVM property java.util.logging.config.file to point to this file.
The configuration file is a standard property file as you know them from Java. Inside this property file you can set properties that configure the various Logger ‘s and Handler ‘s used in your application.
Here is a list of properties you can set in the configuration file. You should double check the JavaDoc over time to see if any of this changes (e.g. in a later version of Java than Java 6).
Property | Description |
handlers | A white space or comma separated list of handler class names to be added to the root Logger |
config | A white space or comma separated list of class names which will be instantiated when the LogManager is initialized. The constructors of these classes can execute arbitrary configuration code. |
«logger».handlers | Sets the handler classes to use for a given Logger in the hierarchy. Replace the «logger» with a specific name of a Logger in your app (e.g. com.jenkov.web). |
«logger».useParentHandlers | Tells a given Logger whether it should log to its parents or not (true or false). |
«logger».level | Tells a given Logger what minimum log level it should log. |
java.util.logging.FileHandler.level | Sets the default log level for all FileHandler ‘s. |
java.util.logging.FileHandler.filter | A class name of the Filter to use on all FileHandler ‘s. |
java.util.logging.FileHandler.formatter | A class name of the Formatter to use on all FileHandler ‘s. |
java.util.logging.FileHandler.encoding | The encoding to use by all FileHandler ‘s (e.g. UTF-8, UTF-16 etc.). |
java.util.logging.FileHandler.limit | The approximate amount of bytes to write to a log file, before rotating to a new file. |
java.util.logging.FileHandler.count | The number of log files to use in the log file rotation. |
java.util.logging.FileHandler.append | Sets whether or not the FileHandler ‘s should append to an existing file or not (true or false), if an existing log file is found. |
java.util.logging.FileHandler.pattern | The log file name pattern. |
java.util.logging.ConsoleHandler.level | Sets the default log level of all ConsoleHandler ‘s. |
java.util.logging.ConsoleHandler.filter | Sets the Filter to use by all ConsoleHandler ‘s |
java.util.logging.ConsoleHandler.formatter | Sets the Formatter to use by all ConsoleHandler ‘s. |
java.util.logging.ConsoleHandler.encoding | Sets the encoding to use by all ConsoleHandler ‘s. |
java.util.logging.StreamHandler.level | Sets the default log level of all StreamHandler ‘s. |
java.util.logging.StreamHandler.filter | Sets the Filter to use by all StreamHandler ‘s |
java.util.logging.StreamHandler.formatter | Sets the Formatter to use by all StreamHandler ‘s. |
java.util.logging.StreamHandler.encoding | Sets the encoding to use by all StreamHandler ‘s. |
java.util.logging.SocketHandler.level | Sets the default log level of all SocketHandler ‘s. |
java.util.logging.SocketHandler.filter | Sets the Filter to use by all SocketHandler ‘s. |
java.util.logging.SocketHandler.formatter | Sets the Formatter to use by all SocketHandler ‘s. |
java.util.logging.SocketHandler.encoding | Sets the encoding to use by all SocketHandler ‘s. |
java.util.logging.SocketHandler.host | Sets the host name of the host to send the log messages to (e.g. jenkov.com). |
java.util.logging.SocketHandler.port | Sets the port number of of the host to send the log message to (e.g. 9999). |
java.util.logging.MemoryHandler.level | Sets the default log level of all MemoryHandler ‘s. |
java.util.logging.MemoryHandler.filter | Sets the Filter to use by all MemoryHandler ‘s. |
java.util.logging.MemoryHandler.size | The size of the internal LogRecord buffer. |
java.util.logging.MemoryHandler.push | The push level of messages causing the buffer to be pushed to the target Handler . Defaults to SEVERE. |
java.util.logging.MemoryHandler.target | The class name of the target Handler . |
Here is an example configuration file. Not all properties are set to a value. You should do that in your own configuration file, or omit the configuration parameter if it has no value.
Note: Configuration properties are applied in the sequence they are listed in the config file. That means, that you should configure parent Logger ‘s before child Logger ‘s. Otherwise the configuration of the parent Logger will override that of the child Logger .
handlers = java.util.logging.FileHandler config = "logger".handlers = "logger".useParentHandlers = "logger".level = java.util.logging.FileHandler.level = WARNING java.util.logging.FileHandler.filter = java.util.logging.FileHandler.formatter = java.util.logging.FileHandler.encoding = java.util.logging.FileHandler.limit = java.util.logging.FileHandler.count = java.util.logging.FileHandler.append = false java.util.logging.FileHandler.pattern = log.%u.%g.txt java.util.logging.ConsoleHandler.level = WARNING java.util.logging.ConsoleHandler.filter = java.util.logging.ConsoleHandler.formatter = java.util.logging.ConsoleHandler.encoding = java.util.logging.StreamHandler.level = WARNING java.util.logging.StreamHandler.filter = java.util.logging.StreamHandler.formatter = java.util.logging.StreamHandler.encoding = java.util.logging.SocketHandler.level = WARNING java.util.logging.SocketHandler.filter = java.util.logging.SocketHandler.formatter = java.util.logging.SocketHandler.encoding = java.util.logging.SocketHandler.host = java.util.logging.SocketHandler.port = java.util.logging.MemoryHandler.level = WARNING java.util.logging.MemoryHandler.filter = java.util.logging.MemoryHandler.size = java.util.logging.MemoryHandler.push = java.util.logging.MemoryHandler.target =
Java Util Logging — Loading logging.properties
Java Util logging uses the configurations which are loaded through one of the options described here . In this tutorial, we will learn different ways to load logging.properties.
Loading logging.properties from absolute path
First let’s get the default properties file (which comes with JDK), e.g. from here:
C:\Java\jdk1.8.0_111\jre\lib\logging.properties
Let’s modify it for our application, we are adding logging level for our application base logger:
handlers= java.util.logging.ConsoleHandler .level= INFO java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter com.logicbig.level = WARNING
Save the above file at local file system e.g. here d:\test-app\logging.properties
Let’s use it in our example:
public class MyClass < private static Logger LOGGER; static < System.setProperty("java.util.logging.config.file", "d:\\test-app\\logging.properties"); //must initialize loggers after setting above property LOGGER = Logger.getLogger(MyClass.class.getName()); >public static void main(String[] args) < System.out.println("-- main method starts --"); LOGGER.info("an info msg"); LOGGER.warning("a warning msg"); LOGGER.severe("a severe msg"); >>
Output
-- main method starts --
May 17, 2017 11:55:44 AM com.logicbig.example.MyClass main
WARNING: a warning msg
May 17, 2017 11:55:44 AM com.logicbig.example.MyClass main
SEVERE: a severe msg
Above output doesn’t show INFO logs, that means our logging.properties setting of com.logicbig.level = WARNING is working as expected.
Note that in these examples, we are setting system properties programmatically, so that we can conveniently set different properties for different main classes in this demo. The right way is to use -DmyPro=value from command line or set it in our IDE run configuration.
Loading logging.properties from classpath
Let’s save the properties file under resource folder:
src/main/resources/logging.properties
handlers= java.util.logging.ConsoleHandler .level= INFO java.util.logging.ConsoleHandler.level = INFO java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter com.logicbig.level = WARNING
The properties file cannot be directly loaded from classpath by using java.util.logging.config.file system property. We have following other options to accomplish that.
By getting absolute location of the classpath resource
public class MyClass2 < private static Logger LOGGER; static < String path = MyClass2.class.getClassLoader() .getResource("logging.properties") .getFile(); System.setProperty("java.util.logging.config.file", path); LOGGER = Logger.getLogger(MyClass2.class.getName()); >public static void main(String[] args) < System.out.println("-- main method starts --"); LOGGER.info("an info msg"); LOGGER.warning("a warning msg"); LOGGER.severe("a severe msg"); >>
Output
-- main method starts --
May 17, 2017 11:55:46 AM com.logicbig.example.MyClass2 main
WARNING: a warning msg
May 17, 2017 11:55:46 AM com.logicbig.example.MyClass2 main
SEVERE: a severe msg
By using LogManager#readConfiguration()
public class MyClass3 < private static Logger LOGGER; static < InputStream stream = MyClass3.class.getClassLoader(). getResourceAsStream("logging.properties"); try < LogManager.getLogManager().readConfiguration(stream); >catch (IOException e) < e.printStackTrace(); >LOGGER = Logger.getLogger(MyClass3.class.getName()); > public static void main(String[] args) < System.out.println("-- main method starts --"); LOGGER.info("an info msg"); LOGGER.warning("a warning msg"); LOGGER.severe("a severe msg"); >>
Output
-- main method starts --
May 18, 2017 10:01:15 AM com.logicbig.example.MyClass3 main
WARNING: a warning msg
May 18, 2017 10:01:15 AM com.logicbig.example.MyClass3 main
SEVERE: a severe msg
Example Project
Dependencies and Technologies Used: