log4j、fileappender 和 tomcat6 日志问题
我看到了很多关于上述主题的问题,但没有一个能解决这个问题。 我正在尝试使用 log4j 记录到 $CATALINA_BASE/logs/ 目录中的自定义文件。 我配置了 log4j.xml 文件并将其复制到 $CATALINA_BASE/lib/ 目录中。
我使用以下几行来创建记录器 -
PatternLayout layout = new PatternLayout(); FileAppender追加器= new FileAppender(“文件名.txt”);
这就是我的问题所在。如何使 FileAppender 采用我在 log4j.xml 中配置的文件名?
我希望它会自动选择它,但是 FileAppender 没有不接受文件名的构造函数。
我是否必须读取 log4j.xml 才能获取文件名?如果是这样,为什么我需要在 xml 中设置该属性呢?
任何帮助将不胜感激。
谢谢, - 瓦斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...为什么要在代码中创建 FileAppender?只需使用 LoggerFactory 创建一个记录器并指定字符串(通常采用您在 log4j XML/properties 文件中配置的 com.xxx.yyy 格式)并开始记录。确保记录器在配置文件中配置为使用 FileAppender 实现(Daily 或 RollingFile),并且一切准备就绪。
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.