Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Adding the FileHandler

Now that we've discussed the theoretical framework, let's look at how we use this in practice. We'll build on top of the SimpleLogging.java example and the mylogging.properties file discussed earlier.

Let's begin by adding a FileHandler to the Logger so that, in addition to the console messages that we saw in the earlier example, we can dump the messages to a log file. For this we'll use the following modified version of our mylogging.properties file:

#---------------------------------------------
#  Default Logging Configuration File
#---------------------------------------------
#  Global properties
#---------------------------------------------
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
#---------------------------------------------
# Default global logging level.
.level= INFO
#---------------------------------------------
# Handler specific properties.
#---------------------------------------------
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %hmycustom%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# 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

We haven't made any changes to our SimpleLogging class. When we run the class, we get an output on the screen similar to the first one. However, we also see a file called mycustom0.log in our home directory. This file contains the XML output of the log records:

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
 <date>2002-06-13T09:57:24</date>
 <millis>1023987444472</millis>
 <sequence>0</sequence>
 <logger>com.informit.SimpleLogging</logger>
 <level>SEVERE</level>
 <class>SimpleLogging</class>
 <method>main</method>
 <thread>10</thread>
 <message>Something really bad happened </message>
</record>
<record>
 <date>2002-06-13T09:57:25</date>
 <millis>1023987445533</millis>
 <sequence>1</sequence>
 <logger>com.informit.SimpleLogging</logger>
 <level>WARNING</level>
 <class>SimpleLogging</class>
 <method>main</method>
 <thread>10</thread>
 <message>This is a warning</message>
</record>
<record>
 <date>2002-06-13T09:57:25</date>
 <millis>1023987445533</millis>
 <sequence>2</sequence>
 <logger>com.informit.SimpleLogging</logger>
 <level>WARNING</level>
 <class>SimpleLogging</class>
 <method>main</method>
 <thread>10</thread>
 <message>This is also a warning</message>
</record>
<record>
 <date>2002-06-13T09:57:25</date>
 <millis>1023987445543</millis>
 <sequence>3</sequence>
 <logger>com.informit.SimpleLogging</logger>
 <level>INFO</level>
 <class>SimpleLogging</class>
 <method>main</method>
 <thread>10</thread>
 <message>Just for your info</message>
</record>
</log>

Let's look at the new entries in the mylogging.properties file. The first modification tells the system to use a FileHandler in addition to the ConsoleHandler that we used earlier:

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler

The next of set of entries specify the properties for the FileHandler. The FileHandler uses the configuration entries that it finds in the LogManager (which reads this file). For all other properties that are not specified, it uses the defaults:

java.util.logging.FileHandler.pattern = %hmycustom%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

java.util.logging.FileHandler.pattern specifies the pattern to be used for generating the output filename. For example, "%h/mycustom%u.log" tells the FileHandler to log the entries in a file in the user's home (%h) directory. "%u" is a unique number to resolve conflicts; the filename will be mycustom{i}.log, where {i} is the index to ensure uniqueness. For other details look at the Javadoc for the java.util.logging.FileHandler class.

java.util.logging.FileHandler.limit specifies an approximate maximum number of bytes to write to the file. If the value specified is zero (the default), there is no limit to the log file size.

java.util.logging.FileHandler.count indicates the number of files to cycle through.

java.util.logging.FileHandler.formatter specifies that the handler should use the XML Formatter class.

Additionally, you can specify properties such as whether the FileHandler should append to a log file, the log Level, and the custom filter to be used.

  • + Share This
  • 🔖 Save To Your Account