Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为“设置绝对路径”;log4j.属性“;文件_Java_Logging_Log4j_Apache Commons Logging - Fatal编程技术网

Java 为“设置绝对路径”;log4j.属性“;文件

Java 为“设置绝对路径”;log4j.属性“;文件,java,logging,log4j,apache-commons-logging,Java,Logging,Log4j,Apache Commons Logging,我正在为我的web应用程序使用ApacheCommons+log4j 通常log4j在类路径中需要一个配置文件;但是我需要将日志配置委托给一个外部文件(我需要在一个环境中部署一个.war,但是日志配置(最大大小、位置等)由另一个团队决定) 我的类路径中有一个commons-logging.properties org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger # log4j.configurati

我正在为我的web应用程序使用ApacheCommons+log4j

通常log4j在类路径中需要一个配置文件;但是我需要将日志配置委托给一个外部文件(我需要在一个环境中部署一个.war,但是日志配置(最大大小、位置等)由另一个团队决定)

我的类路径中有一个commons-logging.properties

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
# log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties
不幸的是,注释行不起作用


有没有办法用外部配置文件设置log4j?

您可以使用jvm参数指示配置文件路径:

-Dlog4j.configuration=absolute path
具有绝对路径的示例:

java -Dlog4j.configuration="file:/dir1/log4j.properties"

您可以使用指示配置文件路径的jvm参数:

-Dlog4j.configuration=absolute path
具有绝对路径的示例:

java -Dlog4j.configuration="file:/dir1/log4j.properties"

将系统属性log4j.configuration=/abslute/或/relative/path\u设置为文件名

commons日志只需要知道它使用的是什么日志实现,它自己的日志实现是以任何方式配置的


这在设置系统属性log4j.configuration=/abslute/or/relative/path_to_file_name中有记录

commons日志只需要知道它使用的是什么日志实现,它自己的日志实现是以任何方式配置的


这记录在

中,您可以将其设置为系统属性
log4j.configuration
属性。例如,在J2SE应用程序中

java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
请注意,该属性值必须是URL

有关详细信息,请阅读中的“默认初始化过程”一节

还可以让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) {
    }
}
import java.util.Enumeration;
导入javax.servlet.*;
公共类SystemPropertieHelper实现
javax.servlet.ServletContextListener{
私有ServletContext上下文=null;
公共void contextInitialized(ServletContextEvent事件){
context=event.getServletContext();
枚举参数=context.getInitParameterNames();
while(params.hasMoreElements()){
String param=(String)params.nextElement();
字符串值=
getInitParameter(param);
if(参数startsWith(“customPrefix”)){
System.setProperty(参数、值);
}
}
}
公共无效上下文已销毁(ServletContextEvent事件){
}
}
然后将其放入web.xml中(context.xml也可以)


customPrefix.property
价值
java.lang.String
servletUtils.SystemPropertieHelper
我从这个监听器代码中得到了这个


我希望这能对您有所帮助!

您可以将其设置为系统属性
log4j.configuration
属性。例如,在J2SE应用程序中

java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
请注意,该属性值必须是URL

有关详细信息,请阅读中的“默认初始化过程”一节

还可以让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) {
    }
}
import java.util.Enumeration;
导入javax.servlet.*;
公共类SystemPropertieHelper实现
javax.servlet.ServletContextListener{
私有ServletContext上下文=null;
公共void contextInitialized(ServletContextEvent事件){
context=event.getServletContext();
枚举参数=context.getInitParameterNames();
while(params.hasMoreElements()){
String param=(String)params.nextElement();
字符串值=
getInitParameter(param);
if(参数startsWith(“customPrefix”)){
System.setProperty(参数、值);
}
}
}
公共无效上下文已销毁(ServletContextEvent事件){
}
}
然后将其放入web.xml中(context.xml也可以)


customPrefix.property
价值
java.lang.String
servletUtils.SystemPropertieHelper
我从这个监听器代码中得到了这个

我希望这能对您有所帮助!

如果您正在使用,您可以使用

您只需使用文件位置(使用)指定参数
log4jconfig
。例如:


log4jConfigLocation
文件:/absolute/path/where/external/logs/are/log4j.properties
log4jRefreshInterval
1000
org.springframework.web.util.Log4jConfigListener
您可以指定
log4jRefreshInterval
,即配置文件刷新检查之间的间隔,以毫秒为单位


有关详细信息,请参阅org.springframework.web.util.log4jwebconfig

如果您正在使用,则可以使用

您只需使用文件位置(使用)指定参数
log4jconfig
。例如:


log4jConfigLocation
文件:/absolute/path/where/external/logs/are/log4j.properties
log4jRefreshInterval
1000
org.springframework.web.util.Log4jConfigListener
您可以指定
log4jRefreshInterval
,即配置文件刷新检查之间的间隔,以毫秒为单位



请参阅
org.springframework.web.util.Log4jWebConfigurer

AFAIK中的更多信息,以这种方式设置配置文件它仍然使用相对路径;您能给我指一个您见过的文档,其中使用了绝对路径吗?回答很好。文件前缀似乎是必需的,至少在unix OS.AFAIK上是这样设置的配置文件仍然使用相对路径;您能给我指一个文档,您看到它使用绝对路径吗?回答很好。文件前缀似乎是必需的,至少在unix操作系统上是这样。不幸的是,在这个项目中,我没有使用spring:\Humm…您只需添加所需的jar文件,无需配置spring。:)不幸的是,在这个项目中,我没有使用spring:\Humm。。。您只需添加