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?

LooMeenin
  • 818
  • 3
  • 17
  • 33
  • possible duplicate of [Java code for getting current time](http://stackoverflow.com/questions/833768/java-code-for-getting-current-time) – OldProgrammer Dec 17 '13 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?? – Ergin Dec 17 '13 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. – LooMeenin Dec 17 '13 at 21:19

1 Answers1

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.

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 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 Dec 17 '13 at 21:47
  • @LooMeenin You should call the super constructor in order to give it a chance to initialize properly. – Stephan Dec 17 '13 at 21:50