views:

266

answers:

1

Hi,

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  A: 

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.

Murali VP
Thanks for the information. What you said makes sense. I tried that and now I do see an attempt to log in the file but the message itself is not logged. The reason I notice an attempt to log is I am using a PatternLayout with a ConversionPattern of <%d >So a new date gets added to the file but the data itself is lost. Have you seen this behavior before?Thanks,-Vas
Here is my log4j.xml file -- <log4j:configuration> <appender name="LogTest" class="org.apache.log4j.RollingFileAppender"> <param name="maxFileSize" value="100KB" /> <param name="maxBackupIndex" value="5" /> <param name="File" value="${catalina.home}/logs/LogTest.txt" /> <param name="threshold" value="debug" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d " /> </layout> </appender> <root> <priority value="debug"></priority> <appender-ref ref="LogTest" /> </root></log4j:configuration>