我正在编写一个部署在Tomcat中的Java Web应用程序,我使用log4j进行日志记录。我喜欢在生成的日志文件名中自动插入Web应用程序的文件夹名。
目前,www.example.com中的文件名设置如下所示log4j.properties:
log4j.appender.R.File=${catalina.home}/logs/mywebapp.log
我需要这样的东西:
log4j.appender.R.File=${catalina.home}/logs/${current.webapp.folder}.log
是否有某种环境变量可以在属性文件中指定,或者我必须从代码中示例化记录器?
3条答案
按热度按时间tag5nh1u1#
您可以使用上下文侦听器来设置系统属性,然后在log4j配置中使用该属性来实现这一点。
步骤1:设置系统属性
您可以首先将系统属性(例如
contextPath
)设置为应用程序的Tomcat上下文路径的值。您可以在上下文侦听器中执行此操作。在web.xml中声明侦听器:
步骤2:使用system属性设置log4j文件名
然后,在log4j配置文件中,可以使用系统属性
contextPath
设置log4j文件名。参考
yxyvkwin2#
这不是一个配置文件唯一的解决方案,所以我保持了一段时间的问题开放。
我已将
log4j.properties
重命名为myapp-log4j.properties
,并将日志文件名称属性修改为:因为我有一个在启动时加载的servlet,所以我在init()函数中初始化log4j。
此函数:
它看起来工作得很好,但我真的不喜欢我必须修改代码中的属性文件。
tjrkku2a3#
首先尝试附加“log4j.properties“。
第二,如果您使用“PropertyConfigurator”...