Friday, October 12, 2012

Simple Log System For Java Application


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fileapp;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;

/**
 *
 * @author sajjad
 */
public class MyLog {

    /**
     * @param args the command line arguments
     */
    private final static Logger LOGGER = Logger.getLogger(FileApp.class.getName());
    public static void main(String[] args) throws IOException {
// LOGGER.setLevel(Level.INFO);

 Calendar now = GregorianCalendar.getInstance();
     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String file=dateFormat.format(date)+".log";
      file="c:/data_log/"+file;
 Handler handler;
        handler =  new FileHandler(file,true);
  handler.setFormatter(new SimpleFormatter() );
 
  LOGGER.addHandler(handler);
  LOGGER.addHandler(new ConsoleHandler());
  LOGGER.info("Our first logging message");
   LOGGER.severe("Something terrible happened");
   }
}