Skip to content

SentryAppender causing warnings #2966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chandrabol opened this issue Sep 19, 2024 · 7 comments
Closed

SentryAppender causing warnings #2966

chandrabol opened this issue Sep 19, 2024 · 7 comments
Labels
duplicate Pull requests or issues with another instance elsewhere

Comments

@chandrabol
Copy link

Description

We have an application that defines custom Log Levels.

When we use the JsonTemplateLayout (https://logging.apache.org/log4j/2.x/manual/json-template-layout.html)
we sometimes get a Null pointer Exception

Configuration

Version: 2.19.0

Operating system: MacOs

JDK: JDK 17

Logs

2024-09-19 11:15:12,451 Log4j2-TF-2-AsyncLoggerConfig-2 ERROR An exception occurred processing Appender Console java.lang.NullPointerException: seq
	at java.base/java.util.Objects.requireNonNull(Objects.java:235)
	at org.apache.logging.log4j.layout.template.json.util.JsonWriter.writeRawString(JsonWriter.java:800)
	at org.apache.logging.log4j.layout.template.json.resolver.LevelResolver.lambda$createNameResolver$3(LevelResolver.java:144)
	at org.apache.logging.log4j.layout.template.json.resolver.LevelResolver.resolve(LevelResolver.java:172)
	at org.apache.logging.log4j.layout.template.json.resolver.LevelResolver.resolve(LevelResolver.java:75)
	at org.apache.logging.log4j.layout.template.json.resolver.TemplateResolver.resolve(TemplateResolver.java:66)
	at org.apache.logging.log4j.layout.template.json.resolver.TemplateResolvers$PrefixedFieldResolverMethod.resolve(TemplateResolvers.java:353)
	at org.apache.logging.log4j.layout.template.json.resolver.TemplateResolvers$MapResolver.resolve(TemplateResolvers.java:409)
	at org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.encode(JsonTemplateLayout.java:302)
	at org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.encode(JsonTemplateLayout.java:58)
	at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:215)
	at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:208)
	at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:199)
	at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:161)
	at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:134)
	at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:125)
	at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:89)
	at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:683)
	at org.apache.logging.log4j.core.async.AsyncLoggerConfig.callAppenders(AsyncLoggerConfig.java:149)
	at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:641)
	at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:624)
	at org.apache.logging.log4j.core.async.AsyncLoggerConfig.log(AsyncLoggerConfig.java:143)
	at org.apache.logging.log4j.core.async.AsyncLoggerConfig.logToAsyncLoggerConfigsOnCurrentThread(AsyncLoggerConfig.java:191)
	at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:112)
	at org.apache.logging.log4j.core.async.AsyncLoggerConfigDisruptor$Log4jEventWrapperHandler.onEvent(AsyncLoggerConfigDisruptor.java:98)
	at com.lmax.disruptor.BatchEventProcessor.processEvents(BatchEventProcessor.java:168)
	at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:125)
	at java.base/java.lang.Thread.run(Thread.java:840)

Reproduction

public static void main(String args[]) {
log.log(EngineLogLevel.AUDIT.getLevel(), Utilities.toJson(auditLog));
}

@vy
Copy link
Member

vy commented Sep 19, 2024

This is a duplicate of #1805, which was fixed in 2.21.0. Would you mind using the most recent Log4j version, i.e., 2.23.0, please?

@vy vy closed this as completed Sep 19, 2024
@vy vy added duplicate Pull requests or issues with another instance elsewhere and removed waiting-for-maintainer labels Sep 19, 2024
@chandrabol
Copy link
Author

@vy After I update the version to 2.23.0,

  1. Custom log level is printed.
  2. Getting the below exception for the log4j2 plugin.

2024-09-19T07:12:03.632479Z main WARN Could not examine class org/apache/logging/log4j/core/async/JCToolsBlockingQueueFactory$MpscBlockingQueue.class java.lang.NoClassDefFoundError: org/jctools/queues/MpscArrayQueue at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017) at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150) at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862) at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760) at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681) at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) at org.apache.logging.log4j.core.config.plugins.util.ResolverUtil.addIfMatching(ResolverUtil.java:462) at org.apache.logging.log4j.core.config.plugins.util.ResolverUtil.loadImplementationsInJar(ResolverUtil.java:357) at org.apache.logging.log4j.core.config.plugins.util.ResolverUtil.findInPackage(ResolverUtil.java:236) at org.apache.logging.log4j.core.config.plugins.util.PluginRegistry.loadFromPackage(PluginRegistry.java:224) at org.apache.logging.log4j.core.config.plugins.util.PluginManager.collectPlugins(PluginManager.java:162) at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:242) at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313) at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:260) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:154) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:46) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:197) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:611) Caused by: java.lang.ClassNotFoundException: org.jctools.queues.MpscArrayQueue at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ... 25 more

@vy
Copy link
Member

vy commented Sep 19, 2024

@chandrabol, typo. 🤦 Mind upgrading to 2.24.0, please? If your problem persists, could you share your pom.xml and log4j2.xml, please?

Edit: You can workaround the issue by adding org.jctools:jctools-core to your dependencies. But, AFAICT, that should not have been needed while upgrading from 2.19.0 to 2.24.0.

@vy vy added waiting-for-user More information is needed from the user and removed waiting-for-maintainer labels Sep 19, 2024
@chandrabol
Copy link
Author

chandrabol commented Sep 19, 2024

Upgraded to 2.24.0.

Getting this message -

2024-09-19T07:28:20.687326Z main WARN The use of package scanning to locate Log4j plugins is deprecated.
Please remove the `packages` attribute from your configuration file.
See https://logging.apache.org/log4j/2.x/faq.html#package-scanning for details.
2024-09-19T07:28:20.687549Z main WARN Some custom `Core` Log4j plugins are not properly registered:
	io.sentry.log4j2.SentryAppender
Please consider reporting this to the maintainers of these plugins.
See https://logging.apache.org/log4j/2.x/manual/plugins.html#plugin-registry for details.

Pom.xml - <log4j2.version>2.24.0</log4j2.version>

Log4j2.xml -

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
    <Properties>
        <Property name="instant">false</Property>
        <Property name="LOG_LEVEL">INFO</Property>
        <Property name="XRAY_LOG_LEVEL">WARN</Property>
        <Property name="AUDIT_LOG_LEVEL">TRACE</Property>
    </Properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <JsonTemplateLayout eventTemplateUri="classpath:logStructure.json" locationInfoEnabled="true"/>
        </Console>
        <Console name="Audit" target="SYSTEM_OUT">
            <PatternLayout pattern='%m%n'/>
        </Console>
        <Sentry name="Sentry" />
    </appenders>

    <Loggers>
        <AsyncLogger name="com.gravty.framework.audit" additivity="false" level="TRACE" includeLocation="true">
            <AppenderRef ref="Audit" level = "${env:AUDIT_LOG_LEVEL}"/>
        </AsyncLogger>
        <AsyncLogger name="com.gravty" additivity="false" level="DEBUG" includeLocation="true">
            <appender-ref ref="Console" level = "${env:LOG_LEVEL}"/>
            <appender-ref ref="Sentry" level="ERROR" />
        </AsyncLogger>
        <AsyncLogger name="com.amazonaws.xray" additivity="false" level="WARN" includeLocation="true">
            <appender-ref ref="Console" level = "${env:XRAY_LOG_LEVEL}"/>
        </AsyncLogger>
        <AsyncRoot level="WARN" additivity="false" includeLocation="true">
            <appender-ref ref="Console"/>
        </AsyncRoot>
    </Loggers>
</configuration>

@github-actions github-actions bot added waiting-for-maintainer and removed waiting-for-user More information is needed from the user labels Sep 19, 2024
@vy
Copy link
Member

vy commented Sep 19, 2024

@chandrabol, could you share the complete pom.xml and log4j2.xml, please? If they happen to contain lines you don't want to disclose, could you trim these files down so that we can reproduce the issue on our side, please?

@ppkarwasz
Copy link
Contributor

@chandrabol,

Can you check if removing the packages attribute from your configuration file removes both warnings. Removing the packages attribute should not remove this warning:

2024-09-19T07:28:20.687549Z main WARN Some custom `Core` Log4j plugins are not properly registered:
	io.sentry.log4j2.SentryAppender
Please consider reporting this to the maintainers of these plugins.
See https://logging.apache.org/log4j/2.x/manual/plugins.html#plugin-registry for details.

If it disappears and everything works, it might be a false positive.

Which version of io.sentry:sentry-log4j2 are you using? Version 7.14.0 properly registers a Log4j plugins.

Are you using shading? See our Shading/Shadowing FAQ entry.

@vy
Copy link
Member

vy commented Sep 19, 2024

@chandrabol, allow me to share some more findings:

The packages attribute

sentry-log4j2 (sadly) recommends using the packages attribute. Though sentry-log4j2:7.14.0 is compiled using Log4j 2.20.0, and hence, contains a Log4j plugin descriptor which removes the need to packages attribute. Hence, you can safely remove packages attribute from your log4j2.xml and it should all be fine.

Asynchronous loggers

I see you're using asynchronous loggers. sentry-log4j2 docs explicitly state the following:

SentryAppender does not support Log4j2's async mode. The Sentry Java SDK itself is already asynchronous and does not perform any blocking operation on the calling thread.

Log4j asynchronous logging is opt-in. That is, you must have extra configuration somewhere (including AsyncLogger and AsyncRoot elements in your pom.xml) to enable it. I advise you to remove all these extras. If performance happens to become a problem, you can configure Log4j in mixed-mode – though I strongly advise you to avoid optimizing, unless you need to.

@vy vy changed the title NPE while using custom Log Levels with JsonTemplateLayout SentryAppender causing warnings Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Pull requests or issues with another instance elsewhere
Projects
None yet
Development

No branches or pull requests

3 participants