log4j 多classloader重复加载配置问题解决

本文介绍了当Log4j被两个不同类加载器初始化时出现的问题及其解决方案。提供了两种解决策略:一是通过配置让Log4j忽略此问题;二是更改初始化Log4j的类加载器。

log4j:ERROR A "org.apache.log4j.xml.DOMConfigurator" object is not assignable to a "org.apache.log4j.spi.Configurator" variable.
log4j:ERROR The class "org.apache.log4j.spi.Configurator" was loaded by
log4j:ERROR [java.net.URLClassLoader@5b0a69d3] whereas object of type
log4j:ERROR "org.apache.log4j.xml.DOMConfigurator" was loaded by [WebappClassLoader
  context:
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@4d405ef7
].
log4j:ERROR Could not instantiate configurator [org.apache.log4j.xml.DOMConfigurator].
log4j:WARN No appenders could be found for logger (com.xxx.xxx.xxx).
log4j:WARN Please initialize the log4j system properly.

错误信息很明显,log4j被两个不同的classloader初始化了两遍结果包错。这个问题不难解决,主要有两种手段:
第一、增加配置,让log4j忽略由不同的classloader的初始化这个问题。在log4j的配置文件中增加

-XX:MaxPermSize=512m -Xmx512m -Denv=DEV -Dlog4j.ignoreTCL=true

即可。在某些版本的log4j的错误信息中,还会提到下面的网址:
http://logging.apache.org/log4j/1.2/faq.html,里面有关注上面属性的介绍。
第二、修改初始化log4j线程的classloader。由于log4j会用当前线程上下文中的classloader去初始化配置,所以在我的沙箱应用中,虽然log4j的jar是由URLClassLoader加载进来的,但是加载的线程还是在WebApp中的,还是WebApp的ClassLoader,所以会出现上面的提示信息。因此,我们至要通过下面代码:

Thread.currentThread().setContextClassLoader(urlclassLoader);

将当前线程的上下文loader改为沙箱的ClassLoader即可。

两种解决方式,你可以按需选择了。

[hadoop@node1 hive]$ tail -100 logs/hiveserver2.log SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:06:00: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:06:24: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:08:08: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:18:27: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:34:00: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:34:40: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) nohup: ignoring input SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/export/server/apache-hive-3.1.3-bin/lib/log4j-slf4j-impl-2.17.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/export/server/hadoop-3.4.0/share/hadoop/common/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory] which: no hbase in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/root/myenv:/export/server/jdk/bin:/export/server/hadoop/bin:/export/server/hadoop/sbin:/home/hadoop/.local/bin:/home/hadoop/bin) 2025-07-20 15:36:56: Starting HiveServer2 Error occurred during initialization of VM java.lang.InternalError: Could not create SecurityManager: allow at sun.misc.Launcher.<init>(Launcher.java:103) at sun.misc.Launcher.<clinit>(Launcher.java:54) at java.lang.ClassLoader.initSystemClassLoader(ClassLoader.java:1444) at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:1429) 这个是hiveserver2日志
最新发布
07-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值