Debug with log topic

When using Pulsar Functions, you can generate logs predefined in functions to a specified log topic and configure consumers to consume messages from the log topic.

For example, the following function logs either a WARNING-level or INFO-level log based on whether the incoming string contains the word danger or not.

  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. import org.slf4j.Logger;
  4. public class LoggingFunction implements Function<String, Void> {
  5. @Override
  6. public void apply(String input, Context context) {
  7. Logger LOG = context.getLogger();
  8. String messageId = new String(context.getMessageId());
  9. if (input.contains("danger")) {
  10. LOG.warn("A warning was received in message {}", messageId);
  11. } else {
  12. LOG.info("Message {} received\nContent: {}", messageId, input);
  13. }
  14. return null;
  15. }
  16. }

As shown in the example, you can get the logger via context.getLogger() and assign the logger to the LOG variable of slf4j, so you can define your desired logs in a function using the LOG variable.

Meanwhile, you need to specify the topic that the logs can be produced to. The following is an example.

  1. bin/pulsar-admin functions create \
  2. --log-topic persistent://public/default/logging-function-logs \
  3. # Other function configs

The message published to a log topic contains several properties:

  • loglevel: the level of the log message.
  • fqn: the fully qualified function name that pushes this log message.
  • instance: the ID of the function instance that pushes this log message.