Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make log4j create log files on demand only?

Tags:

log4j

We have a modular application where modules have their own log4j logs (i.e. communication log and error log). The appenders and categories for these are all configured in the core log4j XML, but not all modules are always installed. The DailyRollingFileAppender creates its file regardless of use and that exposes the full set of modules although not present and as some of them are customer specific we'd like to hide logs not in use. Is there a way to make DailyRollingFileAppender create its file on first use instead of automatically at startup?

like image 574
Mirvnillith Avatar asked Apr 27 '10 08:04

Mirvnillith


1 Answers

I had the same problem, so I have extended the standard FileAppender class and I have created a new LazyFileAppender that is a FileAppender that lazily initialize the log file(creates it only when the first write operation happens).

The LazyFileAppender and some other additions to the standard log4j library can be found into a simple library that I have created : log4j-additions .

You can look at the source to develop your own extension or you can use it as is ...

like image 69
aleroot Avatar answered Oct 23 '22 11:10

aleroot