log4j, fileappender and tomcat6 logs question

935 views Asked by At

I have seen many questions about the above topics but none that address this. I am trying to use log4j to log to a custom file in $CATALINA_BASE/logs/ directory. I configured the log4j.xml file and copied it into the $CATALINA_BASE/lib/ directory.

I use the following lines to create the logger -

PatternLayout layout = new PatternLayout(); FileAppender appender= new FileAppender("filename.txt");

This is where my problem is. How do I make the FileAppender take the file name I configured in the log4j.xml?

I was hoping it will automatically pick that up, but there is no consructor for FileAppender that will not take a filename.

Do I have to read the log4j.xml to get the name of the file? If so why in the world do I need to set that property in the xml at all?

Any help would be greatly appreciated.

Thanks, - Vas

1

There are 1 answers

2
Murali VP On BEST ANSWER

Hmm... why are you creating a FileAppender in code? Just create a logger using LoggerFactory specifying the string (usually in com.xxx.yyy format that you configured in the log4j XML/properties file) and start logging. Make sure the logger is configured to use the FileAppender implementation (Daily or RollingFile) in the config file and you are all set to go.