Jump to content

How does Tracy hook into Errors/Exceptions


gebeer
 Share

Recommended Posts

Hi @adrian Tracy is acting on all Errors/Exceptions (and in strict mode also Warnings) while it is active. May I ask how your module achieves that?

This question is related to 

Basically I want to catch all Errors/Exceptions (+ Warnings, if possible) and act upon them. And I'm struggling in finding a clever way to do so.

In your module I see a hook to 'ProcessWire::trackException'. Would this be a good place to start for Exceptions?

But what about Warnings?

For example, if I wanted to use an external service for error reporting like https://docs.rollbar.com/docs/basic-php-installation-setup, where would I intercept PW Errors to init my custom error handler? 

Link to comment
Share on other sites

Hi @gebeer - the Tracy core handles this via PHP's set_error_handler (https://github.com/nette/tracy/blob/f9645299229878a61b46baa82ff61d8a739bccf8/src/Tracy/Debugger/Debugger.php#L226)

Looking at what your trying to do here, can I suggest that you extend Tracy rather than creating a new module. Recently I added the ability for Tracy to log to Slack (https://github.com/adrianbj/TracyDebugger/blob/3185ec827c001c0c310cb9b13558ca3e53eabbc5/includes/SlackLogger.php) and use different icons based on the error level, eg:
image.thumb.png.123317c4dbee68599150fa5902053cb1.png
so you should certainly be able to do something similar for Rollbar. I feel like most PW users make use of Tracy and this would just help to make it better without needing a separate module.

You might find some further inspiration in the panels that add support for monolog that you can find here: https://componette.org/search/tracy

You might find this useful: https://tracy.nette.org/en/recipes#toc-custom-logger although keep in mind that I had issues with that approach in that I couldn't get it to also support emailing errors at the same time

Let me know if you have any other questions.

  • Like 3
Link to comment
Share on other sites

@adrian Thank you for all the info. That will help a lot.

10 hours ago, adrian said:

the Tracy core handles this via PHP's set_error_handler

will look through your code where Tracy gets started up. That should give me good pointers. Rollbar also uses set_error_handler. Their docs say to init Rollbar at the entry point of the application. I'll find the right place to do this.

10 hours ago, adrian said:

Looking at what your trying to do here, can I suggest that you extend Tracy rather than creating a new module. Recently I added the ability for Tracy to log to Slack

Great pointer. I need this as a standalone module because we want to have different targets to report to based on error levels.. But once I have that, we can add an option to Tracy as well.

Thanks again for all those useful links.

 

  • Like 1
Link to comment
Share on other sites

18 minutes ago, gebeer said:

I need this as a standalone module because we want to have different targets to report to based on error levels

That could be built into Tracy if you want it. Currently the email logging is just all error levels to all listed email addresses, but I am sure we could support different levels per email. Up to you of course but it just feels like a good fit to me.

Excited to see what you do either way.

  • Like 2
Link to comment
Share on other sites

10 hours ago, adrian said:

That could be built into Tracy if you want it. Currently the email logging is just all error levels to all listed email addresses, but I am sure we could support different levels per email. Up to you of course but it just feels like a good fit to me.

Don't think that many people will have a need for this. But would be nice for sure ? Reason why I need a separate module is also that it should be independent of other modules (aka TD). I am using Tracy on every install. But not all devs do that and the agency I am freelancing for would like to have it as a standalone tool.

  • Like 1
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
 Share

  • Recently Browsing   0 members

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