- Why Logging?
- Start Logging
- Changing the Log Level
- Logger Demystified
- Adding the FileHandler
- Adding a Custom Formatter
- Adding a Filter
- Conclusion
Changing the Log Level
Now let's look into how the defaults are set for the Logger object. Look in the jre/lib directory of your Java Runtime Environment installation for a file called logging.properties. This file is used for customizing the logging level and other logging details at runtime. Copy the file into your work directory and call it mylogging.properties. Below is a truncated sample listing of the logging.properties file.
#--------------------------------------------- # Default Logging Configuration File #--------------------------------------------- # Global properties #--------------------------------------------- handlers= java.util.logging.ConsoleHandler #--------------------------------------------- # Default global logging level. .level= INFO #--------------------------------------------- # Handler specific properties. #--------------------------------------------- # Limit the messages 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
Alternatively, you can cut-and-paste the properties specified above and save them as the mylogging.properties file.
As seen above, the logging Level both globally as well as for the specific ConsoleHandler is set to INFO. Change the level as specified below:
.level= ALL java.util.logging.ConsoleHandler.level = FINER
Here we're setting the global Level to ALL (log all message), but for the ConsoleHandler we're choosing the Level as FINER. This implies that the console should ignore messages lower in priority than FINER. To run the application now, use the following command to specify that you want to override the default logging.properties files:
java -Djava.util.logging.config.file=mylogging.properties SimpleLogging
The resulting output on the screen may be as follows:
Jun 12, 2002 6:08:29 PM SimpleLogging main SEVERE: Something really bad happened Jun 12, 2002 6:08:29 PM SimpleLogging main WARNING: This is a warning Jun 12, 2002 6:08:29 PM SimpleLogging main WARNING: This is also a warning Jun 12, 2002 6:08:29 PM SimpleLogging main INFO: Just for your info Jun 12, 2002 6:08:29 PM SimpleLogging main CONFIG: A configuration message Jun 12, 2002 6:08:29 PM SimpleLogging main FINE: A fine message Jun 12, 2002 6:08:29 PM SimpleLogging main FINER: A finer message