import java.util.*;
import java.text.*;
import java.io.*;
public class FileLogger extends SimpleLogger {
private File logFile;
private static FileLogger instance;
public static FileLogger getInstance(String logFilePath){
if (instance == null)
instance = new FileLogger(logFilePath);
return instance;
}
public FileLogger(String logFilePath){
logFile = new File(logFilePath);
try{
logFile.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void log(Level logLevel, String component, String msg)
throws LoggerException {
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = new Date();
if(logLevel.compareTo(getLevel()) >=0)
try{
FileWriter out = new FileWriter(logFile,true);
String dateTime = dateFormat.format(date);
BufferedWriter bufferedWriter = new BufferedWriter(out);
bufferedWriter.append("\n" + dateTime + logLevel + component + msg + "");
bufferedWriter.newLine();
bufferedWriter.close();
}catch (IOException e) {
throw new LoggerException();
}
}
@Override
public void log(Level error, String component, Throwable t)
throws LoggerException{
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = new Date();
if(error.compareTo(getLevel()) >=0)
try{
FileWriter out = new FileWriter(logFile,true);
String dateTime = dateFormat.format(date);
BufferedWriter bufferedWriter = new BufferedWriter(out);
bufferedWriter.append("\n" + dateTime + error + component + t + "");
bufferedWriter.newLine();
bufferedWriter.close();
}catch (IOException e) {
throw new LoggerException();
}
}
}
public enum Level {
DEBUG(1), WARNING(2), SEVERE(3), INFO(4), ERROR(5);
private int level;
private Level(int level)
{
this.level = level;
}
public int getLevel(){
return level;
}
}
public class LoggerException extends Throwable {
/**
*
*/
private static final long serialVersionUID = 1L;
public LoggerException(){
}
}
public class LoggerFactory {
LoggerFactory factory = new LoggerFactory();
public static FileLogger getLogger(String log) {
if(log.equals("File"))
return new FileLogger(log);
else
{
return null;
}
}
}
public class MyProgram {
private SimpleLogger logger;
public static void main(String[] args) {
try {
SimpleLogger logger = LoggerFactory.getLogger("File");
logger.setLevel(Level.INFO);
logger.log(Level.INFO, "main", "system starting");
try {
double res1 = doDivision(2,3);
double res2 = doDivision(3,0);
} catch (Exception e) {
logger.log(Level.ERROR, "main", e);
}
logger.log(Level.INFO, "main", "system ending");
} catch (LoggerException e) {
e.printStackTrace();
}
}
private static double doDivision(int i, int j) {
{
double res=0;
try {
SimpleLogger logger = LoggerFactory.getLogger("File");
// logger.initialize();
logger.log(Level.INFO, "doDivision", "entering");
if ( j == 0)
{
throw new IllegalArgumentException("Attempt to divide by 0");
}
else
{
res = i/j;
logger.log(Level.INFO, "doDivision", "result is " + res);
}
} catch (LoggerException e) {
e.printStackTrace();
}
return res;
}
}
}
public abstract class SimpleLogger {
protected Level level;
public void setLevel(Level l){
level = l;
}
public Level getLevel(){
return level;
}
public abstract void log(Level logLevel, String component, String msg) throws LoggerException;
public abstract void log(Level error, String component, Throwable t) throws LoggerException;
}