April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Application Logs and the Logger Class

  • December 3, 2001
  • By Piroz Mohseni
  • Send Email »
  • More Articles »

Application logs serve different purposes. They can help in debugging, remote troubleshooting, and tracking. The logging requirements usually depend on who is going to use the logs. A developer would need a more detailed log compared to an end-user trying to fix a common problem.

Piroz Mohseni

Another common requirement is the ability to transmit the log (via a protocol such as HTTP) to a remote location. This not only allows supporting customers remotely but also serves as a useful data-gathering mechanism where development teams can create profiles of their application usage and the conditions under which it is failing.

Addressing these requirements, with JDK 1.4, the Java platform will support a set of core logging facilities contained in the java.util.logging package. The main class is Logger. You obtain an instance of Logger using the static getLogger() method with a unique name as its argument:

static Logger logger = Logger.getLogger("com.foo.someClass");

Each Logger has a "Level" associated with it that can be set using the setLevel() method. This reflects a minimum Level that the logger cares about. If a Logger's level is set to null, then its effective level is inherited from its parent. The different levels can be found as fields in the Levels class:

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)

When you look at the "log" methods inside Logger, you will notice they can be grouped into five categories. First are the "log" methods, which take a level, a message, and sometimes additional parameters to the message as arguments. Second are the "logp" methods. They are more precise by taking a source class and method as their arguments. These are the class and method that initiated the logging request. Third are the "logrb" methods, which take a resource bundle as argument to localize the logged message. Fourth, are a series of methods: entering(), exiting(), and throwing(). These methods can be used to log method entrance, method returns, and exceptions that are thrown. Fifth are a series of convenience methods that log a string at a particular level. The method name corresponds to the level name. These include methods like config(), info(), and fine().

In addition to the Logger class, there are a few other features that are interesting. One is the Handler. It takes the logs from Logger and further processes them. A handler has an instance of Formatter associated with it. For example, the XMLFormatter class formats the logs into XML, which is particularly useful for additional processing or sending the log record via SOAP to a central data collection facility.

With the right balance among security, privacy, performance, and customer support, the logging facility provides an effective framework for managing both simple and complex application logs.

The Logging package contains several specific handlers. The ConsoleHandler outputs the log records to System.err. The FileHandler outputs the logs into a file or a set of rotating files. SocketHandler can be used to write to a network stream, which can be used to send the logs to a remote location. You can also create your own specific instances of Handler. A LogRecord is used to pass the actual logging requests between the framework and the specific log handler.

All of this must somehow be managed, and that's where the LogManager class comes into picture. You can retrieve an instance by calling LogManager.getLogManager(). The LogManager object is created during class initialization and cannot subsequently be changed. At startup, the LogManager class is located using the java.util.logging.manager system property. By default, the LogManager reads its initial configuration from a properties file "lib/logging.properties" in the JRE directory. LogManager keeps track of the names and the hierarchy of the various Loggers. In addition, it can specify various configurations for the Loggers that make up the framework.

Including logging information with your application can be very helpful in maintenance and troubleshooting. Using a consistent framework, such as the one offered natively by the Java platform assures cross-platform compatibility. It also allows for extensions and third-party applications to handle logs from different applications. With the right balance among security, privacy, performance, and customer support, the logging facility provides an effective framework for managing both simple and complex application logs.

Have you written a useful class and would like it to be featured as "Class of the Month"? Contact me at mohseni@bita-tech.com.

About the Author

Piroz Mohseni is founder of Bita Technologies, focusing on business improvement through the effective use of technology. His areas of interest include enterprise Java, XML, and e-commerce applications.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel