log4j、fileappender 和 tomcat6 日志问题

发布于 2024-08-12 22:26:22 字数 465 浏览 2 评论 0原文

我看到了很多关于上述主题的问题,但没有一个能解决这个问题。 我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2024-08-19 22:26:22

嗯...为什么要在代码中创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文