yelo3
yelo3

Reputation: 5943

Logback stops logging after clearing log file

I usually clear log files when I'm in developement mode and I need to have a fresh start to focus only on things I have to test.

If I clear a log file in linux (have not tested Windows), logback stops to write to that file Maybe it's something about open handlers and file descriptors in linux.

How can I recover the situation without restarting the application? Is it possibile to have an appender that can automatically recover this situation?

Upvotes: 1

Views: 749

Answers (1)

glytching
glytching

Reputation: 47895

While your application is running (and Logback within your application has an open handle to the log file) ...

  • You won't be able to delete the file on Windows
  • You will be able to delete the file on Linux but as far as Logback is concerned the file still exists until Logback closes that open file handle. So, Logback won't know that the the file has been deleted but since the file has been deleted Logback cannot actually write anything to disk and this situation remains until Logback is re-initialised (and your FileAppender recreates the file). Typically, this will be done on application startup.

There's an open issue against Logback requesting a change in Logback's behaviour in this situation.

If you goal here is to have log output which focusses on recent-activity-only then you could define a rolling file appender with a minimal size and no history just to retain the (for example) last 1MB of data, this might help offer some focus on recent events only.

Alternatively, you'll have to:

  • Vote for this issue
  • Use grep/awk to find the relevant aspects of your log file (you can easily grep on timestamps) even if they are in a log file which contains the last few hours of log events
  • Ease the burden of restarting your application in this scenario by writing a simple script which does something like: (1) stop application; (2) delete log file; (3) start application

Upvotes: 2

Related Questions