LOG4J function

The LOG4J function in ESQL writes a log message by using Apache Log4j 2.x, without requiring a Log4j node in the message flow.

Syntax

Read syntax diagramSkip visual syntax diagramLOG4J( loggerName, logLevel, logMessage )

The LOG4J function writes a log message to the specified Log4j logger at the specified log level. The function uses the Log4j configuration that is defined in the integration server's server.conf.yaml file. The LOG4J function has the following parameters:

loggerName

Type: CHARACTER

The name of the Log4j logger to use. The logger name must correspond to a logger that is defined in the Log4j configuration file (log4j2.xml). Different loggers can have different configurations for output destinations, formats, and filtering.

logLevel

Type: CHARACTER

The severity level at which the log message is written. Valid values are TRACE, DEBUG, INFO, WARN, ERROR, and FATAL.

logMessage

Type: CHARACTER

The text of the message to write to the log.

The LOG4J function returns a BOOLEAN value: TRUE if the log message is written successfully, or FALSE if it fails.

For more information about configuring Log4j for use with IBM® App Connect Enterprise, see Writing application log messages using Log4j.

Examples

The following example logs messages at different severity levels from a Compute node:

CREATE COMPUTE MODULE LogFromESQL_Compute
  CREATE FUNCTION Main() RETURNS BOOLEAN
  BEGIN
    DECLARE logResult BOOLEAN;
    DECLARE orderId CHARACTER;

    SET orderId = CAST(InputRoot.XMLNSC.Order.OrderId AS CHARACTER);

    -- Log at INFO level
    SET logResult = LOG4J('OrderLogger', 'INFO', 'Processing order: ' || orderId);

    -- Log at DEBUG level
    SET logResult = LOG4J('OrderLogger', 'DEBUG',
        'Order details: ' || CAST(InputRoot AS CHARACTER));

    -- Log a warning if the order amount is high
    IF InputRoot.XMLNSC.Order.Amount > 1000 THEN
      SET logResult = LOG4J('OrderLogger', 'WARN', 'High value order: ' || orderId);
    END IF;

    SET OutputRoot = InputRoot;
    RETURN TRUE;
  END;
END MODULE;