问题描述
我正在使用Apache Commons + Log4J作为我的Web应用程序.
通常,log4j需要class路径内的配置文件;但是我需要将记录配置委托给外部文件(我需要在环境中部署.war,但是日志配置(最大大小,位置等)是第二个团队.
我在我的classpath中有一个共识.
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger # log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties
不幸的是,注释的行不起作用.
有没有一种方法可以使用外部配置文件设置log4j?
推荐答案
您可以将其设置为系统属性log4j.configuration属性..例如在J2SE App
中java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
请注意,该属性值必须是URL.
有关更多阅读部分, log4j Manual.
也有可能让ServletContextListener设置系统属性:
import java.util.Enumeration; import javax.servlet.*; public class SystemPropertiesHelper implements javax.servlet.ServletContextListener { private ServletContext context = null; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); Enumeration<String> params = context.getInitParameterNames(); while (params.hasMoreElements()) { String param = (String) params.nextElement(); String value = context.getInitParameter(param); if (param.startsWith("customPrefix.")) { System.setProperty(param, value); } } } public void contextDestroyed(ServletContextEvent event) { } }
然后将其放入您的web.xml(也应该用于context.xml)
<context-param> <param-name>customPrefix.property</param-name> <param-value>value</param-value> <param-type>java.lang.String</param-type> </context-param> <listener> <listener-class>servletUtils.SystemPropertiesHelper</listener-class> </listener>
我从答案.
我希望这可以帮助您!
其他推荐答案
您可以使用指示配置文件路径的JVM参数:
-Dlog4j.configuration=absolute path
具有绝对路径的示例:
java -Dlog4j.configuration="file:/dir1/log4j.properties"
其他推荐答案
设置系统属性log4j.configuration =/abslute/or//everal/path_to_file_name
Commons Loggging仅需要知道其使用的日志记录实现,日志记录实现IT以始终配置的任何方式配置.
这是在 log4j手动中.
问题描述
I am using apache commons + log4j for my web app.
normally log4j needs a configuration file inside the classpath; but I need to delegate the logging configuration to an external file (I need to deploy a .war in an environment, but the log configurations (max size, position, etc) it's up to a second team.
I have a commons-logging.properties in my classpath
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger # log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties
unfortunately, the commented line doesn't work.
Is there a way to set up log4j with an external configuration file?
推荐答案
You can set it as a system property log4j.configuration property .. for example in J2SE app
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
Note, that property value must be a URL.
For more read section 'Default Initialization Procedure' in Log4j manual.
It's also possible letting a ServletContextListener set the System properties:
import java.util.Enumeration; import javax.servlet.*; public class SystemPropertiesHelper implements javax.servlet.ServletContextListener { private ServletContext context = null; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); Enumeration<String> params = context.getInitParameterNames(); while (params.hasMoreElements()) { String param = (String) params.nextElement(); String value = context.getInitParameter(param); if (param.startsWith("customPrefix.")) { System.setProperty(param, value); } } } public void contextDestroyed(ServletContextEvent event) { } }
And then put this into your web.xml (should be possible for context.xml too)
<context-param> <param-name>customPrefix.property</param-name> <param-value>value</param-value> <param-type>java.lang.String</param-type> </context-param> <listener> <listener-class>servletUtils.SystemPropertiesHelper</listener-class> </listener>
I got this from this listener code from answer .
I hope this could help you!
其他推荐答案
You can use a jvm parameter indicating the configuration file path:
-Dlog4j.configuration=absolute path
example with an absolute path:
java -Dlog4j.configuration="file:/dir1/log4j.properties"
其他推荐答案
Set the system property log4j.configuration=/abslute/or/relative/path_to_file_name
commons logging only needs to know what logging implementation it's using, the logging implementation it self is configured in whatever way it is always configured in.
This is documented in the log4j manual.