Java Exceptions Assertions Logging Book

Java Logging

Taken from:

Java Exceptions Assertions Logging Book
Click on the cover to learn more

Configuring Java Logging

You can configure java logging in three different ways:

  1. Use a configuration properties file
  2. Configure logging by providing a class
  3. Setup your own logging configuration


Using a Configuration File

The configuration file for the logging system is located, by default, in the jre/lib directory, and is named as Note that this is the default logging configuration file, and you can specify an alternative filename with the java.util.logging.config.file system property, as shown here:


java –Djava.util.logging.config.file=/temp/


The file defines the loggers, handlers, filters and formatters you want to use in your logging.

Here is a quick summary of how the logging configuration file is organized:


Global Properties

Global properties, of course, apply to all loggers and handlers. A key global property is the default global logging level, which specifies the events the logging system will log for all loggers. You can override this global level with a specific logging level.

You can change the default global logging level by changing the value of the default logging level, which is set to INFO, as shown here:


.level = INFO


Handler Specific Properties

This section specifies the configuration of individual handlers. Here is what you will see by default:


#default file output is in user’s home directory.

java.util.logging.FileHandler.pattern = %h/java%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.FileHandler.level = ALL


java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


As you can see, the logging levels for our two handlers are set to INFO and ALL. The formatter is set to XMLFormatter for the FileHandler. If you wish to change it to SimpleFormatter, include the following line instead:


java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter


Facility Specific Properties

The facility specific properties provide extra configuration control for individual loggers.

For example, you can set a custom logger such as to log just the messages at the SEVERE level, as shown here:


The first part of the file shows how to set up handlers for the root logger. Note that the root logger doesn’t have a name. By default, console logging (ConsoleHandler) is enabled for the root logger:


handlers= java.util.logging.ConsoleHandler


In order to add other handlers, such as a FileHandler to the root logger, include a new line beginning with the word “handlers” and add the FileHandler to the root logger. Here’s how the line should look:

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


Since you cannot separate the names of the handlers, you cannot configure the handlers for different loggers separately. This is so because you must refer to the handlers by their class names.


Logging Levels

Remember that unlike your System.out.println statements, you are going to leave the calls to the loggers in your production application code. To keep overhead to a negligible amount, you must ensure that you log only important log records. The Java logging API offers you a class named Level, which helps you set logging levels. Simply put, logging levels denote the significance of a logging record. Each logging level determines the importance of a particular log message. The class Level determines which messages get written to the log.

Depending on how the Logger’s log level is set, a log message may not be forwarded to a Handler.

You can specify the logging level ALL to trap all log records. You can turn all logging off by choosing the level OFF. JUL provides the following predefined logging levels:

  • Severe: meant for messages with the highest importance, usually fatal errors.
  • Warning: for trapping warning messages.
  • Info: for runtime informational messages.
  • Config: configuration related messages (usually informational).
  • Fine: messages that provide detail, useful in debugging issues.
  • Finer: these messages provide more detail than the FINE level messages.
  • Finest: offers even more detail than the FINER level messages.


You can also define your own custom logging levels if you want.


How to Use the Java API for Logging

The best way to set up Java logging in your code is to create a separate Logger in each class. It is recommended practice to make the instance static and final:


public class Logging examples  {

private static final Logger logger =



Note the following:

  • Making the instance static and final means that all the class’s instances will use the same Logger.
  • You pass the name of the Logger you wish to create as a string parameter to the getLogger() method.
  • You don’t have to, but by convention, the class name and the package name are used as the name of the Logger.


In the example, I used the getName() method which, as is clear, gets you the name of a Logger. Logger offers many other logging methods, which include:


Table 8.1 – Logger’s Methods


log(Level level, String message);

log(Level level, String message, Object param1);

log(Level level, String message, Object[] params);

log(Level level, String message, Throwable t);

log(LogRecord record);


logp(Level level, String sourceClass, String sourceMethod, String msg);

logp(Level level, String sourceClass, String sourceMethod, String msg,

    Object param1);

logp(Level level, String sourceClass, String sourceMethod, String msg,

    Object[] params);

logp(Level level, String sourceClass, String sourceMethod, String msg,

    Throwable t);


logrb(Level level, String sourceClass, String sourceMethod,

    String bundle, String msg);

logrb(Level level, String sourceClass, String sourceMethod,

    String bundle, String msg, Object param1);

logrb(Level level, String sourceClass, String sourceMethod,

    String bundle, String msg, Object[] params);

logrb(Level level, String sourceClass, String sourceMethod,

    String bundle, String msg, Throwable t);


entering(String sourceClass, String sourceMethod);

entering(String sourceClass, String sourceMethod, Object param1);

entering(String sourceClass, String sourceMethod, Object[] params);


exiting(String sourceClass, String sourceMethod);

exiting (String sourceClass, String sourceMethod, Object result);


fine(String message);

finer(String message);

finest(String message);


config(String message);

info(String message);

warning(String message);

severe(String message);

throwing(String sourceClass, String sourceMethod, Throwable t);