How do I properly Log4j, closing all Appenders and, therefore, files

16,410

Solution 1

Try this:

org.apache.log4j.LogManager.shutdown();

However, the problem you are experiencing is strange and shouldn't be happening. What JVM, container, log4j version are you using?

Solution 2

I had the same problem but my solution required two steps:

First, I had to call LogManager.shutdown() from a servlets destroy() method.

Then I had to fix the close() method of our own Appender implementation (which is a subclass of AppenderSkeleton), so that it properly closes the appenders from getAllAppenders().

Share:
16,410
alamar
Author by

alamar

What's interesting to me: Music, Biology, Data (not necessarily Big), Cryptography and Digital Democracy. What I do: Backend, Java, C, Linux, a bit of JavaScript. I am a project management committee member of Apache Ignite project.

Updated on June 04, 2022

Comments

  • alamar
    alamar about 2 years

    My servlet-based web-app sometimes fails to close Log4j log files when redeployed, leaking to file descriptor leakage and occassionally leading to servlet-contained dying from 'too many open files'.

    I have a ContextListener, what should I put into its contextDestroyed() to tell log4j to shut down and release all resources?

    Quick skim over javadocs revealed Hierachery class with shutdown() method. I have no idea on how to actually get the current Hierachery, and the javadoc states this class has no user-serviceable components inside :)