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 6 of 6

Thread: Log() method in need of fixing please

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

    Default Log() method in need of fixing please

    Ok here it is this I'm creating a FileLogger and its a singleton by design, I need to log messages to the file, once I have the new file I need to append the log message to the end of the file. Unfortunately I do not know how to do this and this is the only part that I am missing is these two stinking methods which are clearly empty. So if anyone could lend some assistance it would be greatly appreciated.


    import java.util.*; 
    import java.text.*;
    import java.io.*;
     
     
    public class FileLogger extends SimpleLogger {
     
    	private File logFile;
        private int logEntryNo = 0;
        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();
            }
        }
     
    	public void add(String data){
            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            Date date = new Date();
            String dateTime = dateFormat.format(date);
     
     
            try{
    			if (logFile.createNewFile()){
                    FileWriter fileWriter = new FileWriter(logFile);
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write("[" + dateTime +  "]#" + ++logEntryNo + ": " + data);
                    bufferedWriter.newLine();
                    bufferedWriter.close();
                }
                else{
                    FileWriter fileWriter = new FileWriter(logFile,true);
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write("[" + dateTime +  "]#" + ++logEntryNo + ": " + data);
                    bufferedWriter.newLine();
                    bufferedWriter.close();
                }
     
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    }
     
     
     
    	@Override
    	public void log(Level logLevel, String component, String msg)
    			throws LoggerException {
     
    	}
     
     
     
    	@Override
    	public void log(Level error, String component, Throwable t)
    			throws LoggerException{
    		// TODO Auto-generated method stub
     
    	}
     
     
     
    	}


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

    Default Re: Log() method in need of fixing please

    What does the code do when it is executed? Where does it write to?

    How can the code be tested?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Log() method in need of fixing please

    When a message is logged, the logfile should contain the following information:  A timestamp  The 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
    2
    The logfile should always be appended to, so that log messages from successive invocations of the logged software all appear in the file. This allows developers to compare log messages over time.

    Messages contain correct information
    Message is only logged if it is at the appropriate log level.

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Log() method in need of fixing please

    What does the code do when it is executed? Where does it write to?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Log() method in need of fixing please

    This is what I came up with but I am not sure that I have all the requirements

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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Log() method in need of fixing please

    not sure that I have all the requirements
    Make a list of the requirements and check off those that the code provides. What is left will be what will have to be done.

    There is no way to compile and execute the posted code for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can I get some help fixing this issue?
    By cbplayer in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 14th, 2013, 03:23 PM
  2. Replies: 0
    Last Post: January 28th, 2013, 04:29 AM
  3. Need help fixing
    By brown2292 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 12th, 2012, 12:53 PM
  4. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 05:52 PM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM