简体   繁体   English

如何使用 Pulsar 作为 Log4j2 的 Appender?

[英]How to use Pulsar as an Appender to Log4j2?

I'm trying to set up Apache Pulsar as an appender to log4j2.我正在尝试将 Apache Pulsar 设置为 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.我已经建立了一个玩具示例,其中 Producer 正在记录一条消息,而 Consumer 订阅了 Pulsar 服务器上的同一主题并侦听该消息。 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:制片人.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:消费者.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: 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>

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.您已在 INFO 级别声明了两个记录器 - org.apache.pulsar,在 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.两者都将使用 Pulsar appender 和一个记录小时(包括毫秒)、线程名称、日志级别和记录器名称的布局。 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.我在发送消息时看到了来自 Pulsar 的一个日志,另一个来自你的测试应用程序,它记录了消息。

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

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