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
…15728)

(cherry picked from commit f7635ec)
  • Loading branch information
nlu90 authored and codelipenghui committed Jun 13, 2022
1 parent 7c88bd1 commit 49087fa
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 @@ -35,6 +36,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 @@ -48,15 +54,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 @@ -82,6 +89,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 49087fa

Please sign in to comment.