简体   繁体   English

Log4J ConsoleAppender 不可分配给 Athena JDBC 驱动程序

[英]Log4J ConsoleAppender is not assignable to Athena JDBC Driver

I have trouble getting the Athena JDBC driver logging in place correctly.我无法让 Athena JDBC 驱动程序正确登录。 I'd favour SL4J SimpleLogger, but it seems this factory is not supported, but ust Log4J (just writing this in case you wonder why there's 2 logging framework).我更喜欢 SL4J SimpleLogger,但似乎不支持这个工厂,但只支持 Log4J(只是写这个以防你想知道为什么有 2 个日志框架)。 Unfortunately we do not want to change all current app logging because of one new jdbc driver.不幸的是,由于一个新的 jdbc 驱动程序,我们不想更改所有当前的应用程序日志记录。

When instantiating the logger without Log4J dependency I get a ClassNotFoundException, which is expected and shows Log4J is not included.在不依赖 Log4J 的情况下实例化记录器时,我得到一个 ClassNotFoundException,这是预期的并显示 Log4J 不包括在内。

log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "com.simba.athena.shaded.apache.log4j.Appender" variable.
log4j:ERROR The class "com.simba.athena.shaded.apache.log4j.Appender" was loaded by 
log4j:ERROR [jdk.internal.loader.ClassLoaders$AppClassLoader@368239c8] whereas object of type 
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [jdk.internal.loader.ClassLoaders$AppClassLoader@368239c8].
log4j:ERROR Could not instantiate appender named "stdout".

Maven Dependencies: Maven 依赖项:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.syncron.amazonaws</groupId>
            <artifactId>simba-athena-jdbc-driver</artifactId>
            <version>2.0.2</version>
        </dependency>

log4j.properties log4j.properties

### Appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.Target=System.out  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

### Log Levels 
log4j.rootLogger=info, stdout  

It's kind of strange as both classes ARE loaded with the same classloader, so a classloader hierarchy issue shouldn't be the problem.这有点奇怪,因为两个类都使用相同的类加载器加载,因此类加载器层次结构问题不应该是问题。 I also verified the driver binds Log4J 1.2 as it points to that JavaDocs in some errors.我还验证了驱动程序绑定 Log4J 1.2,因为它在某些错误中指向该 JavaDocs。

Would appreciate any hint from a Log4J expert.感谢 Log4J 专家的任何提示。 Thanks!谢谢!

Here's my findings after some time debugging classloading if someone experioences the same issues:如果有人遇到相同的问题,这是我在调试类加载一段时间后的发现:

The Simba Athena driver provided by AWS comes with Log4J included. AWS 提供的 Simba Athena 驱动程序附带了 Log4J。 The Log4J Apache Classes are repackaged in a "shaded" package: Log4J Apache 类被重新打包在一个“阴影”包中:

com.simba.athena.shaded.apache.log4j.Xyz

These classes will still load the apache configuration as expected like log4.properties file in the resources.这些类仍然会像资源中的log4.properties文件一样按预期加载 apache 配置。 The "standard" Apache Log4J classes will have troubles interoperating with those (eg the apache Appenders cannot be used in the log4j.properties which is loaded by the Athena driver because of this). “标准”Apache Log4J 类在与这些类​​的互操作时会遇到麻烦(例如,Apache Appender 不能用于由 Athena 驱动程序加载的 log4j.properties 中,因此)。

In the case you just want to get around the error messages created by the Athena driver (potentially not even using Log4J in the application like in the case I mentioned) you can address the Classes in the shaded packages within a standard 1.2 Log4J config and remove any Log4J dependencies from the build path.如果您只想绕过 Athena 驱动程序创建的错误消息(甚至可能不像我提到的那样在应用程序中使用 Log4J),您可以在标准 1.2 Log4J 配置中处理阴影包中的类并删除构建路径中的任何 Log4J 依赖项。

Example:例子:

Instead of代替

log4j.appender.Appender1=org.apache.log4j.ConsoleAppender

Use

log4j.appender.Appender1=com.simba.athena.shaded.apache.log4j.ConsoleAppender

I know it's not a perfect solution for all scenarios, but at least you can control errors spilled by the Athena driver this way.我知道这不是适用于所有场景的完美解决方案,但至少您可以通过这种方式控制 Athena 驱动程序溢出的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM