Log4j ScriptFilter for only logging when logger name starts with a certain word?

I am trying to write a log4j2 xml file where some of the appenders will only allow logs if the logger name starts with a specific word, for example “foo”. In this instance I’m using a Kafka filter to write to an event hub.

To get into the weeds a bit — I’m using a shell script to write the xml file and using the shell script as an init file for a databricks compute cluster and then using that cluster to send logs to the event hub instance. But without using a ScriptFilter, the rest of the process works fine in that I’m able to listen to the messages the event hub receives, and the messages are what I would expect to see.

I’ve tried many variations of this ScriptFilter with no luck. Whenever I remove the ScriptFilter, it logs everything, but when I add back in any variation of my ScriptFilter, the event hub stops receiving any messages at all.

Here are some things I’ve tried:

<!-- in Appenders section -->
<Kafka name="kafkaAppender" topic="eventHubInstanceName">
    <JsonTemplateLayout eventTemplateUri="path/to/template.json"/>
    <Property name="bootstrap.servers">event-hub-namespace.servicebus.windows.net:9093</Property>
    <Property name="sasl.mechanism">PLAIN</Property>
    <Property name="security.protocol">SASL_SSL</Property>
    <Property name="sasl.jaas.config">
      org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="[REDACTED]";
    </Property>
    <Filters>
      <ScriptFilter onMatch="ACCEPT" onMismatch="DENY">
      <Script name="scriptFilter" language="javascript"><![CDATA[
         var loggerName = logEvent.getLoggerName();
         loggerName.startsWith("foo");
      ]]></Script>
      </ScriptFilter>
    </Filters>
</Kafka>

I’ve also tried this variation of the Script:

<Script name="scriptFilter" language="javascript"><![CDATA[
  if (logEvent.getLoggerName().startsWith("foo")) {
    return true;
  }
    return false;
  ]]>

and this one:

<Script name="scriptFilter" language="javascript"><![CDATA[
  if (logEvent.getLoggerName().startsWith("foo")) {
    true;
  }
    false;
  ]]>

I’ve also tried moving the filter out of the appender section entirely and adding it to the Root section instead along with the AppenderRef to kafkaAppender, as well as moving the filter into its own Logger and adding the AppenderRef to it that way. Nothing works — whenever I add in a ScriptFilter, nothing logs at all.

I’m unable to use a regex filter here because it only checks the message itself, whereas I want to check the name of the logger.

Please help!