Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.1k views
in Java FTP by
Hi, i have a bit of problem with the logging API, which we just added to our program.

Basically i have a TimerTask class which runs every few mins (24/7) uploading files

Problem #1:
I had OutOfMemoryError after it runs over night if the code is like this. I assumed it's because i keep creating new FileAppender for each run.

public void run()
{
     try {
     Logger.addAppender(new FileAppender("errorLog.log")); 
     } catch (IOException e1) {
     e1.printStackTrace();
     }
     Logger.setLevel(Level.ERROR);

     // DO THE REST OF THE CODE HERE

     Logger.shutdown();
}


Problem #2:

public TimerSchedule()
{
    try {
     Logger.addAppender(new FileAppender("errorLog.log"));
     } catch (IOException e1) {
     e1.printStackTrace();
     }
     Logger.setLevel(Level.ERROR);
}

public void run()
{
      //DO THE REST OF THE CODE HERE
     Logger.shutdown(); --> the new problem
}



So i moved the Logger.addAppender(new FileAppender("errorLog.log")) to the constructor so it wont be recreated every 5 mins, it actually works. I didnt get OutOfMemoryError after running it all night.

But as stated in the code. Logger.shutdown() is now causing the new problem. If didnt invoke the method, the log seemed doubling for every time my program running the task.
1st loop: 1 error
2nd loop: 2 errors
3rd loop: 3 errors
and so on and so on... it keeps doubling

If i invoked Logger.shutdown() at the end of run() it only did the first loop, and thats it, it stop logging.
1st loop: 1 error
2nd loop: stop logging error


Any tips how to do this correctly? Obviously im doing wrong here.

Thank you...

5 Answers

0 votes
by
i just checked the source code

close() on FileAppender class is doing
log.flush() and
log.close()

will it be possible to have additional new method flush() that just does
log.flush()
without closing the PrintWriter


maybe this could solve the problem #2 im having?
0 votes
by (161k points)
You should create the FileAppender exactly once in the TimerTask's constructor or in another class.

shutdown() should only be called when logging is finished, i.e. never in your case.
0 votes
by
You should create the FileAppender exactly once in the TimerTask's constructor or in another class.

shutdown() should only be called when logging is finished, i.e. never in your case.



that's what i did

but the loger keeps appending logs from the previous task

1st loop: found 1 error - displayed 1 error in log file
2nd loop: found 1 error - displayed 3 errors in log file (instead of 2 errors)
3rd loop: found 1 error - displayed 6 errors in log file (instead of 3 errors)

maybe because it wasn't flushed after each run() ?
0 votes
by (161k points)
The FileAppender flushes after each log call.

What are these errors that you are talking about?

You should create the FileAppender exactly once in the TimerTask's constructor or in another class.

shutdown() should only be called when logging is finished, i.e. never in your case.



that's what i did

but the loger keeps appending logs from the previous task

1st loop: found 1 error - displayed 1 error in log file
2nd loop: found 1 error - displayed 3 errors in log file (instead of 2 errors)
3rd loop: found 1 error - displayed 6 errors in log file (instead of 3 errors)

maybe because it wasn't flushed after each run() ?
0 votes
by
The FileAppender flushes after each log call.

What are these errors that you are talking about?

You should create the FileAppender exactly once in the TimerTask's constructor or in another class.

shutdown() should only be called when logging is finished, i.e. never in your case.



that's what i did

but the loger keeps appending logs from the previous task

1st loop: found 1 error - displayed 1 error in log file
2nd loop: found 1 error - displayed 3 errors in log file (instead of 2 errors)
3rd loop: found 1 error - displayed 6 errors in log file (instead of 3 errors)

maybe because it wasn't flushed after each run() ?




oh okay.. thanks for the info...

sorry for not being clear...
the error is just one error i created to test the logging feature.

if it flushes for each log then there must be something wrong with my code. cheers

Categories

...