log4j的默认的配置文件为Log4j.properties,但是,如果这个配置文件为了方便管理,而放在了项目的其他位置,或者是修改了配置文件的默认文件名,这个时候就可以使用指定log4j的配置文件的一种方式,

具体如下:

如果我的项目的文件管理如下图,也就是在编译完以后虽然是在classpath下,但是,log4j的配置文件不在默认指定的位置,

log4j的配置文件不在classpath下面的初始化

 

如果是web项目,则可以初始化log4j,代码片段如下:

public void init() throws ServletException {
        String path = getServletContext().getRealPath("/");
        String file = getInitParameter("log4j").replace(File.separatorChar, '/');
        if (file != null) {
            PropertyConfigurator.configure(path + file);
        }
    }

 

当然,这只是基于servlet的,如果只是一个普通的class,则初始化也大同小异,无非就是拿到log4j的配置文件的所在位置而已,然后再

PropertyConfigurator.configure(log4jConfigerFilePath);

这样,log4j就能够找到配置文件所在位置了。

相关文章:

  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-06-08
  • 2021-09-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案