Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Whats wrong with my program?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my program?

    Objectives:
    create FileLogger, its parent class, and the associated classes the factory class, a LoggerException class that wraps exceptions such as IOException, and an enumeration class for the log levels.

    When a message is logged, the logfile should contain the following information:
    A timestamp
    he log level of the message
    The component that the program was in when the message is logged
    The message itself, or the stack trace of the Throwable (in the case of the second log method

    Message is only logged if it is at the appropriate log level.

    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;
     
    }

    ***OUTPUT***
    10.12.2013 20:01:53INFOmainsystem starting
     
    10.12.2013 20:01:53ERRORmainjava.lang.NullPointerException
     
    10.12.2013 20:01:53INFOmainsystem ending


    --- Update ---

    I'm assuming theres nothing wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Whats wrong with my program?

    I'm assuming theres nothing wrong?
    What is the post for? If you don't know of anything that is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Customer java program( Whats wrong with my code?)
    By Blazta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2013, 03:37 AM
  2. Whats wrong here??
    By cbplayer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2013, 02:19 PM
  3. Please help to understand whats the wrong with my guess. - Simple program
    By rayan2004 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2012, 12:03 AM
  4. Simple sorting program, can't figure out whats wrong with my code
    By USC CSCE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 29th, 2012, 07:38 AM
  5. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM