Jump to content

Module Concept for Error Reporting via Email


Recommended Posts

Hello all,

I'm working on a concept for a module that sends error reports via email to different recipients based on different error levels (notice, warning, error) and I don't know yet how to best tackle this.

It would be preferable if I could use PW internal classes, functions. I had a read through https://processwire.com/blog/posts/debugging-tools-built-in/ and especially the logging section. But looking at related classes like FileLog and WireException, it doen't seem like one can hook into them.

I'm quite tempted to use NetteTracy, like TracyDebugger does. It seems like it can do what I want https://tracy.nette.org/en/guide#toc-production-mode-and-error-logging.

PW is writing errors/exceptions to the log, I just wouldn't know how to tap into this, especially for different log levels.

If anyone can point me in the right direction it would be much appreciated.

  • Like 1
Link to comment
Share on other sites

5 minutes ago, flydev said:

@gebeer Interesting. You want to hook WireLog::___save() and recover the type log on arguments(0)

👉  https://github.com/processwire/processwire/blob/dev/wire/core/WireLog.php#L96-L175

Good find, thank you. By default PW only logs errors, exceptions, not warnings or notices. So the available types would  be "error" and "exceptions". Is there a way of telling PW to also log warnings and notices?

Link to comment
Share on other sites

  • 3 months later...

Thanks again @flydev

I am still investigating this. In wire/core/Wire.php there is a hookable method trackException. This seems to be a good place to hook into for errors/exceptions.

But for Warnings I'm still not 100% sure how to them. To me it seems like WireLog is only taking care of 'error' and 'exception'. Notices has a NoticeWarning class. That looks promising but they seem to only get logged when in debug mode. I will experiment with those findings.

FYI: The module I am planning to build will most likely include an option to send errors to https://rollbar.com/ using https://docs.rollbar.com/docs/basic-php-installation-setup. But also an option to just email them to configurable addresses.


  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

I've been working further on the module concept and found a (mostly reliable) way to react on all error types in PW. Just wanted to share my findings before a release.

Looking through PW core, I found that nowhere PW registers custom error or exception handlers. So I decided to use set_error_handler() for warnings/notices and set_exception_handler() for exceptions. To also cover fatal errors, I hook into WireShutdown::fatalError instead of registering my own shutdown function. Gladly Ryan provides that hook 🙂

So the basis for my operations looks like this in the module's init():

        $this->originalErrorHandler = set_error_handler(array($this, 'errorHandler'), E_NOTICE|E_USER_NOTICE|E_WARNING|E_USER_WARNING);
        $this->originalExceptionHandler = set_exception_handler(array($this, 'exceptionHandler'));
        $this->wire->addHookAfter('WireShutdown::fatalError', $this, 'fatalHandler');

Note, how I save the original error and exception handlers, so I can set them back once I've finished my operations so that my module doesn't interfere with other modules that use their own handlers (like TracyDebugger).

For now I'm only doing the send email based on error type part. The https://rollbar.com/ part will most likely go into a separate module.

I still need to test some on a production site until there will be an alpha/beta release.

Related question: does anybody know how to deliberately cause an E_NOTICE error, other than using trigger_error('My error', E_USER_NOTICE) in PHP > 8?
I couldn't find or think of a real world example. Before PHP 8 we could use "echo $undeclaredVariable". But since 8 those notices are converted to warnings.

  • Like 2
Link to comment
Share on other sites

5 hours ago, gornycreative said:

Perhaps referring to an undeclared index?

Unfortunately not anymore since 8.0. The docs say

  • A number of notices have been converted into warnings:


    • Attempting to read an undefined variable.
    • Attempting to read an undefined property.
    • Attempting to read an undefined array key.
    • Attempting to read a property of a non-object.
    • Attempting to access an array index of a non-array.
    • Attempting to convert an array to string.
    • Attempting to use a resource as an array key.
    • Attempting to use null, a boolean, or a float as a string offset.
    • Attempting to read an out-of-bounds string offset.
    • Attempting to assign an empty string to a string offset.

But thanks for looking into it 🙂


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...