开发者

log4j, fileappender and tomcat6 logs question

开发者 https://www.devze.com 2022-12-12 22:12 出处:网络
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 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.

开发者_StackOverflow中文版

Thanks, - Vas


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消