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.)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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 isbut
LogLog
itself is hard-coded to useSystem.out.println
for these messages so the only possibility I can see is to switch on debugging (-Dlog4j.debug=true
) and somehow hook in toSystem.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.
If you can setup your own Configurator you can do something like that:
Setup the JAVA system property : -Dlog4j.configuratorClass=MyConfigurator And then have your configurator instance intercepts the doConfigure call.
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: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.By means of AspectJ it is pretty simple to catch that method invocation:
In prose this code means:
OptionConverter.selectAndConfigure
is called,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. :-)
Edit: 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.
Insert this into your java call:
Thats all.