How can you find what URL was used in log4j default initialization?

后端 未结 4 413
执笔经年
执笔经年 2021-01-03 06:44

Log4j default initialization goes through a procedure to find and use a URL to configure with. Afterward, how can you find out what URL was ultimately used, without having

4条回答
  •  悲哀的现实
    2021-01-03 07:12

    The procedure used is hard-coded in a static initializer block in LogManager, so there doesn't appear to be a way to hook into it. The only place where it tells you what's going on is

    LogLog.debug("Using URL ["+url+"] for automatic log4j configuration.");
    

    but LogLog itself is hard-coded to use System.out.println for these messages so the only possibility I can see is to switch on debugging (-Dlog4j.debug=true) and somehow hook in to System.setOut before log4j is initialized, then parse the debug log message. But that is probably even more fragile than coding the config procedure yourself.

    Even then, there may have been other programmatic configuration applied after the default configuration procedure (e.g. a Spring Log4jConfigListener) - there isn't necessarily a single configuration URL as such.

    It might be worth putting in a log4j feature request to factor out the config file search code into a static method that you can call from elsewhere, but that wouldn't help when you might have to cope with earlier versions of log4j.

提交回复
热议问题