<div style='background-color: none transparent;'></div>
Home » » Creating a Log file

Creating a Log file

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

public class ErrorLog
{
private static ErrorLog singleton=new ErrorLog();
private PrintWriter writer;
private static SimpleDateFormat sdf = null;
private static String dateaTime = null;

private ErrorLog()
{
String dirPath="D:/tests";
try
{
if (dirPath!=null)
{
writer = new PrintWriter(new FileOutputStream("D:/tests/file.log", true), true);
}
else
{
System.out.println("Fatal error, can~t create error log.");
}
}
catch (IOException exp)
{
System.out.println("Fatal error, can~t create error log - "+exp.getMessage());
exp.printStackTrace();
}
}


public static synchronized void log(String toLog)
{
try
{
sdf = new SimpleDateFormat("yyyy~ ~MM~ ~dd~ ~ hh:mm:ss aaa");
dateaTime = sdf.format((Calendar.getInstance().getTime()));

System.out.println("dateaTime"+dateaTime);
singleton.writer.write(dateaTime+" --> ");
singleton.writer.write(toLog);
singleton.writer.write("\n");
singleton.writer.flush();
}
catch (Exception exp)
{
System.out.println("Fatal error, can~t write to log - "+exp.getMessage());
exp.printStackTrace();
}
}
}

/*
usage: ErrorLog.log("my string message");
*/


Share this article :
 
Copyright © 2011. B.Sc B.Tech MCA Ploytechnic Mini,Main Projects | Free Main Projcets Download | MCA |B.tech . All Rights Reserved
Company Info | Contact Us | Privacy policy | Term of use | Widget | Advertise with Us | Site map
Template Modify by Creating Website. Inpire by Darkmatter Rockettheme Proudly powered by Blogger