Using Custom ClassLoader to Isolate Log42j and Slf4j. Console log: Properties contains an invalid element or attribute "property" #3960
-
|
env:
RequirementsI want to develop an Agent to trace service status. To isolate Log42j and Slf4j, I develop a custom classloader which named LogClassLoader. The main requirements are:
Code and ProblemLogClassLoader override loadClass() method. When it works, if the name has prefix about Log4j2 or Slf4j, I will load class from the specific directory. the code about LogClassLoader is copy from apache skywalking AgentClassLoader. The code of LogClassLoader is: My log4j2.xml is : In order to load jar and log4j2.xml from specific directory, I use Class.forName(className, true, LogClassLoader) to load class. And I use reflection api to invoke getContext(ClassLoader.class, boolean.class, URI.class) method to load log4j2.xml, which avoid use the configuration in AppClassLoader's classpath. And the code is: When it works, I find it can load jar and log4j2.xml from the specific directory. The result is it cannot replace the variables of Appenders , although the variables are defined in properties element.
The config can work as expected in springboot's project. The full demo project is: https://github.com/[zhanjinhao/log-learn](https://github.com/zhanjinhao/log-learn) and it can reproduce the problem. Run Application in springboot way, the console will output "Properties contains an invalid element or attribute "property"" and the file will be created in ${FILE_PATH} not in "agent_logs" DEBUGI try to debug the code to find some reason. in org.apache.logging.log4j.core.config.AbstractConfiguration#doConfigure, the children of Properties is empty.
In springboot project, the children is an List which contains three children.
But i don't know why the children are different. I don't have knowledge about xml parsing. Can somebody give me some advice! Thank a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @zhanjinhao, I took a look at your I've made some changes to demonstrate this, which also include a check for already-loaded classes and support for parallel-capability. You can review the code here :) I've confirmed that with this approach, |
Beta Was this translation helpful? Give feedback.




Hi @zhanjinhao,
I took a look at your
ClassLoaderand it seems that since it doesn't implementfindResource, resources can only be found in the parent classloader. I believe you can create a much simpler implementation by leveragingURLClassLoader.I've made some changes to demonstrate this, which also include a check for already-loaded classes and support for parallel-capability. You can review the code here :) I've confirmed that with this approach,
app_logsandagent_logsare separated correctly.