Log Debug Messages in a User Defined Function for Confluent Cloud for Apache Flink¶
When you create a user defined function (UDF) with Confluent Cloud for Apache Flink®, you have the option of logging events to help with monitoring and debugging. Your log messages appear in the Confluent Cloud Console’s statement log page.
For more information on creating UDFs, see Create a User Defined Function.
Limitations¶
UDF logging has these limitations.
- Log4j logging only: External UDF loggers can be composed only with the Apache Log4j logging framework.
- Burst rate to 1000/s: UDF logging supports up to 1000 log events per second for each UDF during a short burst of high activity. This helps to optimize performance and to reduce noise in logs. Events that exceed the maximum rate are dropped.
Implement logging code¶
In your UDF project, import the org.apache.logging.log4j.LogManager
and
org.apache.logging.log4j.Logger
namespaces. Get the Logger
instance by calling the LogManager.getLogger()
method.
package your.package.namespace;
import org.apache.flink.table.functions.ScalarFunction;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Date;
/* This class is a SumScalar function that logs messages at different levels */
public class LogSumScalarFunction extends ScalarFunction {
private static final Logger LOGGER = LogManager.getLogger();
public int eval(int a, int b) {
String value = String.format("SumScalar of %d and %d", a, b);
Date now = new java.util.Date();
// You can choose the logging level for log messages.
LOGGER.info(value + " info log messages by log4j logger --- " + now);
LOGGER.error(value + " error log messages by log4j logger --- " + now);
LOGGER.warn(value + " warn log messages by log4j logger --- " + now);
LOGGER.debug(value + " debug log messages by log4j logger --- " + now);
return a + b;
}
}
The following log levels are supported.
- OFF
- FATAL
- ERROR
- WARN
- INFO
- DEBUG
- TRACE
- ALL
View logged events¶
After the instrumented UDF statements run, you can view logged events in the Confluent Cloud Console’s event logging page.