1

My app deployed on Tomcat uses log4j to write a log file. If I delete that file, then the app does not recreate it. I also tried to recreate it manually, but it remains always empty. Is there any way to delete the log file (not from the app), create a new one in the same path with the same name, and that it can be written by the application?

abarazal
  • 369
  • 6
  • 19
  • 1
    Why you need this? – Jens Jan 17 '17 at 14:55
  • @Jens Because it's another program that deletes the log file if it's not written within 2 days. – abarazal Jan 17 '17 at 14:58
  • And this makes sence? – Jens Jan 17 '17 at 15:00
  • @Jens Yes, for me. The other program behaviour is out of my scope. – abarazal Jan 17 '17 at 15:05
  • 1
    On a linux environent instead of deleting the logfile you can do something like `cat /dev/null >logfile` and it will be cleared without removing the file handle in your logging framework – Jens Jan 17 '17 at 15:08
  • I do not see the point of negative voting this question. – abarazal Jan 17 '17 at 15:08
  • @abaraza - I suspect because the downvoters think you are trying to fix the wrong problem. The real problem is the other program that is deleting the log file. And how to get it fixed. – Stephen C Jan 17 '17 at 22:34
  • Possible duplicate of [Log4j does not recreate files on deletion](https://stackoverflow.com/questions/9937796/log4j-does-not-recreate-files-on-deletion) – Lambart May 25 '17 at 19:47

1 Answers1

0

Is there any way to delete the log file (not from the app), create a new one in the same path with the same name, and that it can be written by the application?

Nope. You need to get the application itself to restart logging.

The problem is that the log4j appender still has a handle for the deleted file, and will continue to write to it ... unaware that it has been deleted.

A better approach would be to have the application itself take care of "rotating" the logfile. Look at the classes that implement the log4j Appender interface for some ideas.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I though that but I still have the hope there is any workaround...thank you anyway. – abarazal Jan 17 '17 at 15:07
  • 1
    Well, there possibly are. For example, you could write a custom appended that checked if the log file existed before each attempt to write. But it is ugly and inefficient ... and the application needs to be changed to make it work. – Stephen C Jan 17 '17 at 22:41