0

The 'logger.info(message);' line in my code creates this output:

Dec 17, 2013 12:54:50 PM greenhouse.GreenhouseControls$ControllerException

I would like to print that line to a text file using a PrintWriter. This is what I have so far:

public ControllerException(String message, int errorcode) {
          super(message);
          this.errorcode=errorcode;   

          Logger logger = Logger.getLogger(ControllerException.class.getName());
          logger.info(message);

          String fileName = "error.log"; 

          try {
            PrintWriter outputStream = new PrintWriter(fileName);
            outputStream.println("Time: " + ????);         // Not sure what to put here..
            outputStream.println("Reason: " + message);
            outputStream.close();
           } catch (FileNotFoundException e) {
            e.printStackTrace();
           }      
       }

What can I include in the print line statement in place of the question marks to achieve this?

3
  • possible duplicate of Java code for getting current time Commented Dec 17, 2013 at 21:06
  • What are you trying to accomplish? The outputStream.println("Reason: " + message); line should print Dec 17, 2013 12:54:50 PM greenhouse.GreenhouseControls$ControllerException to your file, is that not happening? What do you want to show up for the Reason and Time parts?? Commented Dec 17, 2013 at 21:07
  • @Ergin I guess I don't need two print statements. I could print the 'Time' and 'Reason' on the same line. I just haven't been able to get the date/time to print to the file at all. So, I would like it to print Dec 17, 2013 12:54:50 PM greenhouse.GreenhouseControls$ControllerException to the file. Commented Dec 17, 2013 at 21:19

1 Answer 1

1

I would do this:

public ControllerException(final String message, int errorcode) {
    super(message);
    this.errorcode=errorcode;

    Logger logger = Logger.getLogger(ControllerException.class.getName());

    logger.setFilter(new Filter() {
        @Override
        public boolean isLoggable(LogRecord record) {
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd',' yyyy HH:mm:ss a");
            String fileName = "error.log";

            try {
                PrintWriter outputStream = new PrintWriter(fileName);
                outputStream.println("Time: " + sdf.format(new Date(record.getMillis()))); 
                outputStream.println("Reason: " + message);
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            return true;
        }
    });

    logger.info(message);
}

This way you get the exact date of the log record.

Sign up to request clarification or add additional context in comments.

2 Comments

I noticed you had 'super(message);' and 'this.errorcode=errorcode;' commented out in the first draft so I removed them and my program runs fine. Should I include them again?
@LooMeenin You should call the super constructor in order to give it a chance to initialize properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.