log4j设置默认日志目录

log4j设置默认日志目录,log4j,Log4j,我的桌面应用程序使用log4j 1.2.17 我允许通过属性文件进行配置,这样用户就可以设置任意数量的日志输出 在生产中,我有一个特定的日志目录,它位于/var/myapp/log目录下 在开发过程中,我在/users/agostino/development/myapp/devenvironment/log下有一个特定的日志目录,它允许我轻松地进行自动测试,因为所有的东西都在一个地方 在log4j.properties中,我有这样一行: log4j.appender.default_file.

我的桌面应用程序使用log4j 1.2.17
我允许通过属性文件进行配置,这样用户就可以设置任意数量的日志输出
在生产中,我有一个特定的日志目录,它位于
/var/myapp/log
目录下
在开发过程中,我在
/users/agostino/development/myapp/devenvironment/log
下有一个特定的日志目录,它允许我轻松地进行自动测试,因为所有的东西都在一个地方
在log4j.properties中,我有这样一行:

log4j.appender.default_file.File=example.log
正如我所说,这应该放在指定的日志目录中,即生产时的
/var/myapp/log
,或开发时的
/users/agostino/development/myapp/devenvironment/log


我只能考虑在属性中进行一些搜索和替换,即使这样也会有点麻烦。有没有更干净的方法?
我不想使用插值,因为它应该对用户透明。

我认为解决问题的一种方法是在虚拟机启动时使用参数。e、 g:

java -Dcustom.log.file=/var/myapp/log ...
然后在配置文件中使用该系统属性。e、 g:

log4j.appender.default_file.File=${custom.log.file}/example.log

您可以通过使用listener来实现它

示例代码:

public class Log4jListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        ServletContext context = sce.getServletContext();
        System.setProperty("rootPath", context.getRealPath(File.separator));

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}
在上面的代码中,部署路径被设置为system property.xml,并在log4j.xml中使用${rootPath}

--您必须在web.xml中声明该侦听器

<!-- listener to set  deployed path to logger -->
  <listener>
        <listener-class>a.b.c.Log4jListener</listener-class>
    </listener>

a、 b.c.Log4jListener

不幸的是,我的应用程序是一个桌面应用程序(刚刚更新了问题)。我可以根据我的环境调整此解决方案吗?