简体   繁体   English

使用 Pulsar 作为 Log4j2 的附加程序:如何停止记录确认?

[英]Using Pulsar as appender to Log4j2: How to stop logging acknowledgements?

I have a Producer that logs messages using Apache Pulsar as an appender to Log4j2.我有一个Producer ,它使用 Apache Pulsar 作为 Log4j2 的附加程序来记录消息。

The Consumer is listening on the same Pulsar topic and writing the received messages to a log file. Consumer正在监听同一个 Pulsar 主题并将接收到的消息写入日志文件。 The Consumer is able to receive the messages properly, but the problem is it's also logging other stuff with it too: Consumer能够正确接收消息,但问题是它也在记录其他内容:

[localhost/127.0.0.1:6650] Received cmd LOOKUP_RESPONSE
Received Broker lookup response: Connect
[coinflex_logs][coinflex_logs_sub] Subscribing to topic on cnx [id: 0xb78fd850, L:/127.0.0.1:53782 - R:localhost/127.0.0.1:6650]
[localhost/127.0.0.1:6650] Received cmd SUCCESS
[id: 0xb78fd850, L:/127.0.0.1:53782 - R:localhost/127.0.0.1:6650] Received success response from server: 2
[coinflex_logs][coinflex_logs_sub] Subscribed to topic on localhost/127.0.0.1:6650 -- consumer: 0
[coinflex_logs] [coinflex_logs_sub] Adding 1000 additional permits
[localhost/127.0.0.1:6650] Received cmd MESSAGE
[id: 0xb78fd850, L:/127.0.0.1:53782 - R:localhost/127.0.0.1:6650] Received a message from the server: org.apache.pulsar.common.api.proto.PulsarApi$CommandMessage@2dab32d1
[coinflex_logs][coinflex_logs_sub] Received message: 3828/25
SSE4.2 CRC32C provider initialized
[coinflex_logs_sub] [aa9ca] processing message num - 0 in batch
[coinflex_logs_sub] [aa9ca] enqueued messages in batch. queue size - 0, available queue size - 2147483647
15:23:59.459 [main] DEBUG com.pulsar_logging_producer.Producer -==========TEST MESSAGE=============

[coinflex_logs_sub] [aa9ca] can ack message to broker 3828:25:-1:0, acktype Individual, cardinality 0, length 1
[coinflex_logs_sub] [aa9ca] acknowledging message - 3828:25:-1:0, acktype Individual
[ConsumerBase{subscription='coinflex_logs_sub', consumerName='aa9ca', topic='coinflex_logs'}] Flushing pending acks to broker: last-cumulative-ack: -1:-1:-1 -- individual-acks: []
[localhost/127.0.0.1:6650] Received cmd PING
[[id: 0xb78fd850, L:/127.0.0.1:53782 - R:localhost/127.0.0.1:6650]] Replying back to ping message
[[id: 0xb78fd850, L:/127.0.0.1:53782 - R:localhost/127.0.0.1:6650]] Sending ping message
[localhost/127.0.0.1:6650] Received cmd PONG

Would like the log to just be:希望日志只是:

15:23:59.459 [main] DEBUG com.pulsar_logging_producer.Producer -==========TEST MESSAGE=============

Consumer.java:消费者.java:

public class Consumer {

    org.apache.pulsar.client.api.Consumer<byte[]> consumer;
    PulsarClient client;
    private final String SERVICE_URL = "pulsar://localhost:6650";
    private static final Logger logger = LogManager.getLogger(Consumer.class);

    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();
            logger.trace(new String(msg.getData()));

            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);
            }
        }
    }

    public void close() {
        try {
            consumer.close();
            client.close();
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }
}

Producer.java:制片人.java:

public class Producer {

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

    public static void main(String[] args) {

        String msg = "==========TEST MESSAGE=============";
        System.out.printf("Message sent: %s\n", msg);
        logger.debug(msg);
    }
}

log4j2.xml for Producer:生产者的 log4j2.xml:

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

Turns out my problem was because I'm always defaulting to the root logger, so pulsar messages get logged there too.原来我的问题是因为我总是默认使用根记录器,所以脉冲星消息也被记录在那里。 All I needed to do was make the root logger log to console and the Consumer logger to log to the rollingFileAppender :我需要做的就是让根记录器记录到控制台,让Consumer记录器记录到rollingFileAppender

<Configuration status="WARN">
    <Properties>
        <Property name="LOG_DIR" value="logs"/>
    </Properties>
    <Appenders>
        <Console name="consoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%msg%n"/>
        </Console>
        <RollingFile
            name="rollingFileAppender"
            fileName="${LOG_DIR}/coinflex_v2.log"
            filePattern="${LOG_DIR}/coinflex_v2_%i.log"
        >
            <PatternLayout pattern="%msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="consoleAppender"/>
        </Root>
        <Logger name="com.pulsar_logging_consumer.Consumer" level="debug" additivity="false">
            <AppenderRef ref="rollingFileAppender"/>
        </Logger>
    </Loggers>
</Configuration>

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

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