Skip to content

Commit

Permalink
[Function] provide default error handler for function log appender (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nlu90 authored Jun 13, 2022
1 parent eac8f7c commit f7635ec
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.logging.log4j.core.ErrorHandler;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.DefaultErrorHandler;
import org.apache.pulsar.client.api.CompressionType;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
Expand All @@ -34,6 +35,11 @@
* to a log topic.
*/
public class LogAppender implements Appender {

private static final String LOG_LEVEL = "loglevel";
private static final String INSTANCE = "instance";
private static final String FQN = "fqn";

private PulsarClient pulsarClient;
private String logTopic;
private String fqn;
Expand All @@ -47,15 +53,16 @@ public LogAppender(PulsarClient pulsarClient, String logTopic, String fqn, Strin
this.logTopic = logTopic;
this.fqn = fqn;
this.instance = instance;
this.errorHandler = new DefaultErrorHandler(this);
}

@Override
public void append(LogEvent logEvent) {
producer.newMessage()
.value(logEvent.getMessage().getFormattedMessage().getBytes(StandardCharsets.UTF_8))
.property("loglevel", logEvent.getLevel().name())
.property("instance", instance)
.property("fqn", fqn)
.property(LOG_LEVEL, logEvent.getLevel().name())
.property(INSTANCE, instance)
.property(FQN, fqn)
.sendAsync();
}

Expand All @@ -81,6 +88,12 @@ public ErrorHandler getHandler() {

@Override
public void setHandler(ErrorHandler errorHandler) {
if (errorHandler == null) {
throw new RuntimeException("The log error handler cannot be set to null");
}
if (isStarted()) {
throw new RuntimeException("The log error handler cannot be changed once the appender is started");
}
this.errorHandler = errorHandler;
}

Expand Down

0 comments on commit f7635ec

Please sign in to comment.