Custom Logging Statements

Custom Logging Statements

IntroductionLogging evaluation of conditional logicLogging field validation errorsLogging saved valuesWriting to the core logWriting to the add-on log

Note: As of Gravity Forms 2.4, the following non-dismissible notice will be displayed on every WordPress admin page when logging is enabled:

Refer to our Logging and Debugging article for more information.
Introduction
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.
12345678add_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.
1234567891011add_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.
12345678910add_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:
1GFCommon::log_debug( __METHOD__ . '(): running.' );
To also include a variable which contains a string in the logging statement you could do this:
1GFCommon::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:
1GFCommon::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:
1$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.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注