Home > Software Development > Simple Logging in Java Application

Simple Logging in Java Application

January 21st, 2013 Leave a comment Go to comments

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.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

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++)
    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):


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.

Categories: Software Development Tags: ,
  1. January 21st, 2013 at 23:37 | #1

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

  2. January 22nd, 2013 at 00:27 | #2

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

  3. January 22nd, 2013 at 23:09 | #3

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

  1. January 22nd, 2013 at 01:48 | #1
  2. January 22nd, 2013 at 22:21 | #2
  3. October 5th, 2014 at 14:49 | #3