Search code examples
javalog4j2apache-pulsar

How to use Pulsar as an Appender to Log4j2?


I'm trying to set up Apache Pulsar as an appender to log4j2. There isn't much documentation for it, but I manage to find a few examples here.

I've set up a toy example where a Producer is logging a single message and a Consumer is subscribed to the same topic on the Pulsar server and listening for the message. The Consumer receives something, but not the message I was expecting:

Message sent: Test message
Message received: 18:05:27.510 [pulsar-client-io-5-1] INFO  org.apache.pulsar.client.impl.ConsumerImpl
Message received: 18:05:27.510 [main] INFO  com.pulsar_logging.Producer

Producer.java:

package com.pulsar_logging;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.pulsar.client.api.PulsarClientException;

public class Producer {

    private static final Logger logger = LogManager.getLogger(Producer.class);

    public static void main(String[] args) {
        Consumer consumer = null;
        try {
            consumer = new Consumer("my-topic", "my-subscription");
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }

        String msg = "Test message";
        System.out.printf("Message sent: %s\n", msg);
        logger.info(msg.getBytes());

        try {
            consumer.listen();
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }
}

Consumer.java:

package com.pulsar_logging;

import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;

public class Consumer {

    org.apache.pulsar.client.api.Consumer<byte[]> consumer;
    PulsarClient client;
    private final String SERVICE_URL = "pulsar://localhost:6650";

    public Consumer(String topic, String subName) throws PulsarClientException {
        client = PulsarClient.builder()
                .serviceUrl(SERVICE_URL)
                .build();
        consumer = client.newConsumer()
                .topic(topic)
                .subscriptionName(subName)
                .subscribe();
    }

    public void listen() throws PulsarClientException {
        while (true) {
            Message msg = consumer.receive();

            try {
                System.out.printf("Message received: %s\n", new String(msg.getData()));
                consumer.acknowledge(msg);
            } catch (Exception e) {
                System.out.println("MESSAGE FAILED");
                consumer.negativeAcknowledge(msg);
            }
        }

    }
}

log4j2.xml:

<Configuration status="WARN">
    <Properties>
        <Property name="DefaultPattern" value="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}"/>
    </Properties>
    <Appenders>
        <Pulsar name="Pulsar_Appender" serviceUrl="pulsar://localhost:6650" topic="my-topic" avoidRecursive="false">
            <PatternLayout pattern="${DefaultPattern}"/>
        </Pulsar>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.pulsar" level="INFO"/>
        <Root level="debug">
            <AppenderRef ref="Pulsar_Appender"/>
        </Root>
    </Loggers>
</Configuration>

Solution

  • You never said what you were expecting but it seems to be doing exactly what you told it to do.

    You have declared two loggers - org.apache.pulsar at level INFO and the root logger at DEBUG. Both will use the Pulsar appender with a layout that logs the hour (including milliseconds), the thread name, the logging level and the logger name. You did NOT specify that the message should be included so it wasn't.

    I see one log from Pulsar while sending the message and another from you test application where it logged the message.