Simple Logging in Java Application

Logging is a common requirement for application. In Java world, there are a few frameworks, the first and the most famous of which is the log4j from Apache foundation. Java included its own logging APIs afterwards. You can find many discussions which one is better in terms of use of use, flexibility, and performance. To reconcile the two APIs, a common abstraction called Simple Logging Facade for Java (SLF4J) was created. As SLF4J works with both log4j and Java logging, you can switch between them easily (in theory).

I think for most applications the requirement on logging is relatively simple. Whatever frameworks out there should satisfy the needs. The real question is mostly the development cost, which is related to the ease of use, prior experiences, and communities. I read and played with both log4j and Java logging before, and quickly forget details.

Recently I read logging again. I found most tutorials out there have good introductions about the framework itself but not enough on how to effectively use it. It’s always nice to get overall concepts and features in a framework. Even more so is to how to use the framework which tends to be difficult sometimes.

Here is a sample shows usage of logging APIs together with external configurations:

 

/**
 * Copyright Steve Jin, doublecloud.org
 */
import java.util.Date;
import java.util.logging.Logger;

public class LoggerTest
{
  private final static Logger LOG = Logger.getLogger(LoggerTest.class.getName());
  
  public static void main(String[] args)
  {
    Date start = new Date();
    for(int i=0; i<100000; i++)
    {
      LOG.severe("severe");
      LOG.warning("warning");
      LOG.info("info");
      LOG.config("config");
      LOG.fine("fine");
      LOG.finer("finer");
      LOG.finest("finest");
    }
    Date end = new Date();
    LOG.severe("total time:" + (end.getTime() - start.getTime()));
  }
}

 

As you see, I added a few line to try out the performance with changes from configuration files listed below. The basic idea is to log right level of details – not too much to overwhelm your system, neither too little to show useful information about the application. In general, you have less logs for production systems.

In terms of configuration, you can use LogManager to do many things, but that is not a clean way to do. The better way is to use properties files as follows:

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

.level= ALL
#LoggerTest.level = WARNING

# default file output is in user's home directory.
java.util.logging.FileHandler.level = ALL
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.SimpleFormatter
# java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

When you run your Java application, you can attach such a configuration file with JVM parameters (you can include this in Java command line):

-Djava.util.logging.config.file=log_debug.properties

You can prepare several properties files for different logging requirement, and use one of them in your specific requirement.

This is not a comprehensive introduction on the logging APIs, but good enough for most logging requirements. From there you can figure out more about advanced handlers, filtering, formatter if really needed. Unless it’s key differentiator for an application, just use standard logging APIs.

This entry was posted in Software Development and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted January 21, 2013 at 11:37 pm | Permalink

    Simple Logging in Java Application http://t.co/o6J25rVl via @sjin2008

  2. Posted January 22, 2013 at 12:27 am | Permalink

    Simple Logging in Java Application (DoubleCloud) http://t.co/JSkooguv

  3. Posted January 22, 2013 at 11:09 pm | Permalink

    Simple Logging in Java Application – http://t.co/B94OXaoF

3 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • NEED HELP?


    My consulting helps clients with virtualization and cloud computing, including VMware infrastructure automation and orchestration, vSphere management APIs, and deep product integration with hypervisors. Current training offerings include vSphere APIs training, vCenter Orchestrator training, and etc. Should you, or someone you know, need these consulting services or training, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.