您如何找到log4j默认初始化中使用的URL? [英] How can you find what URL was used in log4j default initialization?

查看:166
本文介绍了您如何找到log4j默认初始化中使用的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Log4j默认初始化通过一个过程来查找和使用用于配置的URL.之后,您如何找出最终使用的URL,而不必自己编写相同的过程? (如果您必须自己编写代码,则可能无法获得与log4j完全相同的代码,并且在将来的发行版中它可能会更改.)

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 to code the same procedure yourself? (If you have to code it yourself, you might not get it exactly the same as log4j does, and also it might change in a future release.)

推荐答案

如果您愿意使用AspectJ LTW(加载时编织),则可以看看Ian Roberts提到的LogManager的静态初始化.在 log4j 1.2.14 中,它看起来像这样:

If you are willing to use AspectJ LTW (load-time weaving), you can take a look at the static initialisation of LogManager mentioned by Ian Roberts. In log4j 1.2.14 it looks like this:

static {
    // (...)
    // if there is no default init override, then get the resource
    // specified by the user or the default config file.
    if (override == null || "false".equalsIgnoreCase(override)) {
        // (...)
        URL url = null;

        // (...)    
        // If we have a non-null url, then delegate the rest of the
        // configuration to the OptionConverter.selectAndConfigure method.
        if (url != null) {
            LogLog.debug("Using URL [" + url + "] for automatic log4j configuration.");
            OptionConverter.selectAndConfigure(
                url, configuratorClassName, LogManager.getLoggerRepository()
            );
        } else {
            LogLog.debug("Could not find resource: [" + configurationOptionStr + "].");
        }
    }
}

很明显,如果可以确定默认URL,则将在静态块内的某个位置调用OptionConverter.selectAndConfigure(URL, ..),以便使用该URL初始化 log4j .

Obviously if a default URL could be determined then OptionConverter.selectAndConfigure(URL, ..) will be called at one point within the static block in order to initialise log4j with that URL.

通过 AspectJ 的方法,捕获该方法调用非常简单:

By means of AspectJ it is pretty simple to catch that method invocation:

import java.net.URL;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.LogManager;

public aspect Log4jAspect {
    before(URL defaultURL) :
        within(LogManager) &&
        cflow(staticinitialization(LogManager)) &&
        call(* OptionConverter.selectAndConfigure(URL, ..)) &&
        args(defaultURL, ..)
    {
        System.out.println("log4j default URL = " + defaultURL);
    }
}

在散文中,此代码表示:

In prose this code means:

  • 如果我们在LogManager类之内,并且
  • 在静态类初始化的控制流之内,并且
  • OptionConverter.selectAndConfigure被称为
  • 然后捕获第一个参数(URL)和
  • 将其打印到控制台(您也可以做其他事情).
  • If we are within class LogManager and
  • within the control flow of the static class initialisation and
  • OptionConverter.selectAndConfigureis called,
  • then capture the first argument (the URL) and
  • print it to the console (you could just as well do something else).

如果没有默认URL,则不会打印任何内容.无需打印URL,您可以将其分配给任何类或您喜欢的类的静态成员.

If there is no default URL, nothing will be printed. Instead of printing the URL you could assign it to a static member of any class or whatever you like.

这是针对您的问题的解决方案,我对其进行了测试,并且可以正常工作.即使解决方案使用的是您没有想到的技术,我也很高兴收到悬赏解答您的问题.但这解决了问题. :-)

This is a solution for your problem, I tested it and it works. I would be happy to receive the bounty for answering your question, even though maybe the solution uses a technology you did not have in mind. But it solves the problem. :-)

编辑:即使我认为没有必要,也可以在没有默认URL的情况下显式拦截日志调用.我只想提一下.

It is also possible to explicitly intercept the log call in the case that no default URL is found, even though I do not think it is necessary. I just wanted to mention it.

这篇关于您如何找到log4j默认初始化中使用的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆