Using Gravity Forms ‘gform_logging_message’ PHP filter

The gform_logging_message filter allows you to customize the messages sent to Gravity Forms logging. You can use this filter to disable default logging behavior or modify the formatting and content of logging messages.

Usage

add_filter('gform_logging_message', 'your_custom_function', 10, 5);

function your_custom_function($message, $message_type, $plugin_setting, $log, $GFLogging) {
    // your custom code here
    return $message;
}

Parameters

  • $message (string): The current logging message.
  • $message_type (string): The current logging message type.
  • $plugin_setting (array): The logging setting for the plugin.
  • $log (object): The KLogger instance.
  • $GFLogging (object): The Gravity Forms Logging object.

More information

See Gravity Forms Docs: gform_logging_message

Examples

Log messages to system’s error logs

Override the default logging behavior and log all enabled Gravity Forms logging statements to your system’s error logs.

add_filter('gform_logging_message', 'gform_debug_logging_stdout', 10, 5);

function gform_debug_logging_stdout($message, $message_type, $plugin_setting, $log, $GFLogging) {
    error_log($message);
    return false;
}

Customize log message format

Modify the formatting of logging messages by adding a timestamp and custom prefix.

add_filter('gform_logging_message', 'gform_custom_logging_format', 10, 5);

function gform_custom_logging_format($message, $message_type, $plugin_setting, $log, $GFLogging) {
    $timestamp = date('Y-m-d H:i:s');
    return "[{$timestamp}] MyPrefix: {$message}";
}

Log only error messages

Filter out all logging messages except for errors.

add_filter('gform_logging_message', 'gform_log_only_errors', 10, 5);

function gform_log_only_errors($message, $message_type, $plugin_setting, $log, $GFLogging) {
    if ($message_type == 'error') {
        return $message;
    }
    return false;
}

Remove sensitive data from log messages

Redact sensitive information, such as email addresses, from logging messages.

add_filter('gform_logging_message', 'gform_redact_sensitive_data', 10, 5);

function gform_redact_sensitive_data($message, $message_type, $plugin_setting, $log, $GFLogging) {
    return preg_replace('/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/', '***REDACTED***', $message);
}

Log messages to a custom file

Save logging messages to a custom file instead of the default Gravity Forms log files.

add_filter('gform_logging_message', 'gform_custom_log_file', 10, 5);

function gform_custom_log_file($message, $message_type, $plugin_setting, $log, $GFLogging) {
    $custom_log_file = '/path/to/your/custom/logfile.log';
    file_put_contents($custom_log_file, $message . PHP_EOL, FILE_APPEND);
    return false;
}

Note: Make sure to replace /path/to/your/custom/logfile.log with the correct path to your custom log file.