6

Whenever a php error/exception occurs, I'd like to log the error and optionally send an email off to the site admin. Is there an easy way to do this with Drupal 7? Maybe an existing module or a combination of existing modules?

I have a long, non-drupal technical background, and I've done this many times over in other frameworks and custom stacks (mostly Python). Not that all frameworks should be created equal, but I figured I'd ask if there was a module for this before going off and developing my own.

I feel like I should be able to use the standard "recent log messages" for my purposes, but two things would need to be added to it. First, I would need to log php exceptions. Second, I would need to have the ability to send these log messages in an email. Now, I understand that logging php exceptions to the "recent log message" table wouldn't be totally all inclusive since the logging php code itself could have a bug in it. But if that was my only only limitation, I'll settle for the 95% solution.

Any direction at all will be appreciated.

6
+25

I'm glad this question was asked some time ago, and Thanks to @coderama for offering a bounty for this. I make questionnaires/challenges to Druplars in my local community, and this was a question there.

PHP allows you to set error and exception handlers. This similar to how you register class autoloaders and shutdown functions.

See set_error_handler

Sets a user function (error_handler) to handle errors in a script.

This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using trigger_error()).

Also, you can set an exception handler to handle uncaught exceptions. See set_exception_handler

Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

Drupal takes over these two when it initializes the environment. even before bootstrapping advanced bootstrap levels, such as the database, variables, and full bootstraps.

If the error happens in Drupal-handled request, it will be handled by either of these handlers (error or uncaught exception handler) unless it was overriden by other modules (Devel's error handler, and other error handling modules or server-side implementations, possibly New Relic).

Both of these functions call _drupal_log_error() to proceed to the logging behavior in Drupal.

The default error handling (and other logging) functionality is based on the watchdog() function.

watchdog() function invokes hook_watchdog(), which the db_log module implements hook_watchdog to store them in the database. watchdog() function itself is not storing them in the database. Similarly, there is syslog module that can store them in the system log (instead of, or in addition to the database log).

If you want to call your own logging/reporting tool, you can simply create a module and implement hook_watchdog() to log them. Note that watchdog() is called to log events that you probably don't want to log, such as users logging in, node CUD events, user registrations, etc. You can filter them down by inspecting the parameters passed for the hook implementation.

3

Drupal supports logging exceptions, although I am not sure if it will automatically log all uncaught exceptions if a module neglects to handle them.

For email alerts, I would look at Monolog or Logging and alerts. Assuming the log contains everything you want, those modules will let you send an email if a log entry is above a certain severity.

2

Check out the Logging and Alerts module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.