Thursday, February 19, 2009

JSP : Write Log Datewise

Writing logs in JSP..

Here is the code you can use to write log Every day Datewise(dd-mm-yyyy).
You can use in in you code JSP or Servlet.

you need to import following

import java.util.*;
import java.text.*;
import java.io.*;

Add following lines in your code accordingly


// open file to write date wise logs
SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
java.util.Date date = new java.util.Date();
String today = df.format(date);

//Provide file in which log to be written
BufferedWriter outlog = outlog = new BufferedWriter(new FileWriter("D:\\Log_"+today+".log", true));

outlog.write("1st Line \n");........... //used "\n" as line seperator
outlog.write("2nd Line ");
outlog.newLine(); .....................//used outlog.newLine() as line seperator
outlog.write("3rd Line ");


//flush BufferedWriter at the end
outlog.flush();
outlog.close();