Skip to main content

Webhook Error Logger

This simple Error Logger enables you to record form submission errors using a webhook.

This guide will walk you through the setup process and show you how to store and analyze errors in a log file. This feature is especially useful for debugging and maintaining reliable form workflows.

Before you begin, make sure to create a logs folder in your site directory. This folder will store the log files generated by the error logger.

Template config

To enable error logging for a form, add the following configuration to your blueprint file. This will trigger the error_logger handler whenever a form.error event occurs:

webhooks:
  - handler: error_logger
  events:
    - form.error

Handler

Next, define the error_logger handler in your site/config.php file. This handler will log errors to a file named form-errors.log inside the logsdirectory:

return [
    'philippoehrlein.kirby-email-manager' => [
        'webhooks' => [
            'handlers' => [
                'error_logger' => function($event, $data) {
                    $logDir = kirby()->root('site') . '/logs';
                    $logFile = $logDir . '/form-errors.log';
                    
                    $logEntry = sprintf(
                        "[%s] Form Error | Message: %s | Data: %s\n",
                        date('Y-m-d H:i:s'),
                        $data['error'],
                        json_encode($data['data'], JSON_PRETTY_PRINT)
                    );

                    file_put_contents($logFile, $logEntry, FILE_APPEND);
                }
            ]
        ]
    ]
];

The error_logger function generates a log entry containing:

  • A timestamp.
  • The error message.
  • The data associated with the form submission.

Example Output

Here’s an example of what a logged error might look like in the form-errors.log file:

[2024-12-06 15:27:36] Form Error | Message: The property "from" is required | Data: {
  "name": "John Doe",
  "email": "john@example.com"
}

This log entry indicates that the from property was missing in the form submission. The detailed data helps you quickly identify and resolve the issue.

By following this guide, you can effectively track and debug form submission errors. This logging system ensures that you stay informed about issues and can address them proactively to maintain a seamless user experience.

For more advanced logging or integrations, consider extending the error_logger handler to send notifications or trigger other workflows.