Wednesday, February 3, 2010

Java Logger with Custom Formatter

In production or development environment, log files are playing major role to identify the nature of the issue and how often it raises. This information reduces the number of question needed to ask customer to understand the issue. Most valuable and precised data has to be captured in log files.

Developers, some time log there native language based log information and product also may shipped with these log information to customer. These kind of non-english or non-locale logs makes customer to panic and get back to vendors ;). Software developers and Quality Assurance folks has to spend considerable effort in stopping these kind of log details.

JAVA logger comes as part of JAVA SE and helps us to easy to configure and store/retrieve log information. Major components in Logger are

Level
specifies levels of information needed to be logged. In production, we may need only SEVERE information. In Development, we may need all information for debugging purpose, we may need to set FINEST or ALL. Possible log levels are OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST, and ALL
LogRecord
Log level and message forms log record and it internally keeps time of record creation
Formatter
This component decides, in what format data has to go to log file. In JAVA SE, java.util.logging.XMLFormatter is default one
Handler
this holds the details of where to store and how to rotate and etc.,
Filter
filter decides given logrecord is logable or not
Logger
Exposes factory method to create Logger object for given name
Exposes APIs to log messages in various level.

XMLFormatter and custom formatter used to show how the log records are created in log file in /tmp/ folder.


import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;

public class LoggerTest {

 public static void main(String[] args) throws IOException {
  final Logger log = Logger.getLogger("LoggerTest", null);
  log.setLevel(Level.FINEST);
  log.setUseParentHandlers(false);
  FileHandler handler = new FileHandler("/tmp/log.log", false);
  log.addHandler(handler);

  // XML formatted log
  handler.setFormatter(new XMLFormatter());
  log.log(Level.FINEST, "Hello world");

  // Custom formatted log
  handler.setFormatter(new Formatter() {
   @Override
   public String format(LogRecord record) {
    return " custom log - " + log.getName() + " : "
      + record.getMillis() + ":" + record.getMessage();
   }
  });

  log.log(Level.FINEST, "Hello world");
 }
}

No comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com