Logging Example in Java

import java.util.logging.*;
import java.io.*;


public class TestLog {


public static Logger logger;


static {
    try {
      boolean append = true;
      FileHandler fh = new FileHandler("TestLog.log", append);
      //fh.setFormatter(new XMLFormatter());
      fh.setFormatter(new SimpleFormatter());
      logger = Logger.getLogger("TestLog");
      logger.addHandler(fh);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
}


public static void main(String args[]) {
    logger.severe("my severe message");
    logger.warning("my warning message");
    logger.info("my info message");
    }
}


Output :



2005-02-28 21:19:28 TestLog main
GRAVE: my severe message
2005-02-28 21:19:28 TestLog main
ATTENTION: my warning message
2005-02-28 21:19:28 TestLog main
INFO: my info message


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email