<a id="flink-sql-enable-udf-logging"></a>

# 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](../operate-and-deploy/monitor-statements.md#flink-sql-monitor-statements-event-logging) page.

For more information on creating UDFs, see
[Create a User-defined Function](create-udf.md#flink-sql-create-udf).

## Limitations

UDF logging has these limitations.

- **Log4j logging only**: You can compose external UDF loggers 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 optimizes
  performance and reduces noise in the logs. Flink drops events that exceed
  the maximum rate.

<a id="flink-sql-enable-udf-logging-add-log-code"></a>

## Implement logging code

### Java UDF

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.

```java
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.

- ALL
- DEBUG
- ERROR
- FATAL
- INFO
- OFF
- TRACE
- WARN

### Python UDF

TODO

<a id="flink-sql-enable-udf-logging-view"></a>

## View logged events

After the instrumented UDF statements run, you can view logged events in the
Confluent Cloud Console’s
[event logging](../operate-and-deploy/monitor-statements.md#flink-sql-monitor-statements-event-logging) page.

## Related content

- [User-defined Functions](../concepts/user-defined-functions.md#flink-sql-udfs)
- [Create a User-defined Function](create-udf.md#flink-sql-create-udf)
- [confluent flink artifact create](https://docs.confluent.io/confluent-cli/current/command-reference/flink/artifact/confluent_flink_artifact_create.html)
- [CREATE FUNCTION Statement](../reference/statements/create-function.md#flink-sql-create-function)

#### NOTE
This website includes content developed at the [Apache Software Foundation](https://www.apache.org/)
under the terms of the [Apache License v2](https://www.apache.org/licenses/LICENSE-2.0.html).
