merlimat commented on a change in pull request #1316: Introduce a pulsar log4j2 
appender
URL: https://github.com/apache/incubator-pulsar/pull/1316#discussion_r171930624
 
 

 ##########
 File path: 
pulsar-log4j2-appender/src/main/java/org/apache/pulsar/log4j2/appender/PulsarManager.java
 ##########
 @@ -0,0 +1,135 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.log4j2.appender;
+
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+import java.util.function.BiFunction;
+import java.util.function.Supplier;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.pulsar.client.api.ClientBuilder;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageBuilder;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+
+public class PulsarManager extends AbstractManager {
+
+    static Supplier<ClientBuilder> PULSAR_CLIENT_BUILDER = () -> 
PulsarClient.builder();
+
+    static BiFunction<String, byte[], Message<byte[]>> MESSAGE_BUILDER = (key, 
data) -> {
+        MessageBuilder<byte[]> messageBuilder = MessageBuilder.create()
+            .setContent(data);
+        if (null != key) {
+            messageBuilder = messageBuilder.setKey(key);
+        }
+        return messageBuilder.build();
+    };
+
+    private PulsarClient client;
+    private Producer<byte[]> producer;
+
+    private final String serviceUrl;
+    private final String topic;
+    private final String key;
+    private final boolean syncSend;
+
+    public PulsarManager(final LoggerContext loggerContext,
+                         final String name,
+                         final String serviceUrl,
+                         final String topic,
+                         final boolean syncSend,
+                         final Property[] properties,
+                         final String key) {
+        super(loggerContext, name);
+        this.serviceUrl = Objects.requireNonNull(serviceUrl, "serviceUrl");
+        this.topic = Objects.requireNonNull(topic, "topic");
+        this.syncSend = syncSend;
+        this.key = key;
+    }
+
+    @Override
+    public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
+        if (producer != null) {
+            try {
+                producer.close();
+            } catch (PulsarClientException e) {
+                // exceptions on closing
+            }
+        }
+        return true;
+    }
+
+    public void send(final byte[] msg)  {
+        if (producer != null) {
+            String newKey = null;
+
+            if(key != null && key.contains("${")) {
+                newKey = 
getLoggerContext().getConfiguration().getStrSubstitutor().replace(key);
+            } else if (key != null) {
+                newKey = key;
+            }
+
+            Message<byte[]> message = MESSAGE_BUILDER.apply(newKey, msg);
+            if (syncSend) {
+                try {
+                    producer.send(message);
+                } catch (PulsarClientException e) {
+                    LOGGER.error("Unable to write to Pulsar in appender [" + 
getName() + "]", e);
+                }
+            } else {
+                producer.sendAsync(message)
+                    .exceptionally(cause -> {
+                        LOGGER.error("Unable to write to Pulsar in appender [" 
+ getName() + "]", cause);
+                        return null;
+                    });
+            }
+        }
+    }
+
+    public void startup() throws Exception {
+        try {
+            client = PULSAR_CLIENT_BUILDER.get()
+                .serviceUrl(serviceUrl)
+                .build();
+            producer = client.newProducer()
+                .topic(topic)
+                .producerName("pulsar-log4j2-appender-" + topic)
+                .enableBatching(true)
+                .batchingMaxPublishDelay(1, TimeUnit.MILLISECONDS)
 
 Review comment:
   If we use batching (with `sendAsync()`) we could increase this delay, since 
most likely it won't be latency sensitive.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to