bookmark_borderCustom Logging Statements

While Gravity Forms and its add-ons include numerous logging statements sometimes you may need to add more. For example, standard logging statements don’t include the evaluation of conditional logic rules or field validation failures.

Find out how to enable logging with Gravity Forms 2.2+ in the logging and debugging article, older versions of Gravity Forms would require the logging add-on.

Logging evaluation of conditional logic

If you are having an issue with notifications not being sent due to the conditional logic rules not being met you can see exactly how the conditional logic rule is configured and what the actual field value was by using the gform_is_value_match hook in your theme functions.php file.

add_filter( 'gform_is_value_match', 'log_conditional_logic_evaluation', 20, 6 );
function log_conditional_logic_evaluation( $is_match, $field_value, $target_value, $operation, $source_field, $rule ) {
	GFCommon::log_debug( 'gform_is_value_match: $rule => ' . print_r( $rule, 1 ) );
	GFCommon::log_debug( 'gform_is_value_match: $field_value => ' . print_r( $field_value, 1 ) );
	GFCommon::log_debug( 'gform_is_value_match: $is_match => ' . var_export( $is_match, 1 ) );

	return $is_match;
}

Logging field validation errors

Field validation failures can be logged using the gform_validation hook in your theme functions.php file.

add_filter( 'gform_validation', 'log_validation_errors', 50 );
function log_validation_errors( $validation_result ) {
    $form = $validation_result['form'];
    foreach ( $form['fields'] as $field ) {
        if ( $field->failed_validation ) {
            GFCommon::log_error( "form #{$form['id']}: validate() - failed: {$field->label}({$field->id} - {$field->type}) - message: {$field->validation_message}" );
        }
    }

    return $validation_result;
}

Logging saved values

Values being saved can be logged using the gform_save_field_value hook in your theme functions.php file.

add_filter( 'gform_save_field_value', 'log_saved_values', 50, 5 );
function log_saved_values( $value, $entry, $field, $form, $input_id ) {

    $input_name = 'input_' . str_replace( '.', '_', $input_id );

    GFCommon::log_debug( "log_save_field_value: Input ID: {$input_id}. POST value => " . print_r( rgpost( $input_name ), true ) );
    GFCommon::log_debug( 'log_save_field_value: Saved value => ' . print_r( $value, true ) );

    return $value;
}

Writing to the core log.

To add a simple custom logging statement to your own code which will save to the core log you use the following:

GFCommon::log_debug( __METHOD__ . '(): running.' );

To also include a variable which contains a string in the logging statement you could do this:

GFCommon::log_debug( __METHOD__ . "(): Nothing to do for the {$event} event." );

If you wanted to output another type of variable such as an array you could use the following:

GFCommon::log_debug( __METHOD__ . '(): form => ' . print_r( $form, true ) );

Writing to the add-on log.

If you are creating your own add-on which uses the Add-on Framework you can use the following to write the logging statement to the log file for the add-on:

$this->log_debug( __METHOD__ . '(): feed => ' . print_r( $feed, true ) );

__METHOD__ is a PHP constant, it will be replaced with the class method name. You could remove that and include the function name or some other identifier in the logging statement itself.