Jump to content

How to debug / use the error log


MadeMyDay
 Share

Recommended Posts

Hello everybody,

just digging deeper in PW and stumbling upon the question how to debug modules etc. There is an errorlog.txt generated and filled on exceptions (I think), but how can I debug variables and functions in modules? Is there an API for that? Like $error->log($foo, $bar) or something? I don't think that my way of var_dump($foo); break; is the way to go ;-)

Thanks in advance,

Marc

Link to comment
Share on other sites

Marc, when you are developing a site it's good to turn debug mode on. This will ensure that errors are sent to the screen, exceptions reported, etc. This can be found in /site/config.php. By default, it is false. You'll want to change it to true:

$config->debug = true; 

Obviously you don't want this enabled for production sites, so remember to change it back to false for live/production sites.

I don't see any problem with using var_dump, var_export, print_r, etc. so long as you are directing that output to where you can see it. Also avoid running these functions on PW objects as you may get into an infinite loop. Sometimes it can be difficult to track the output of these functions because PW performs a redirect after most POSTs. But if you use PW's built-in reporting functions, they will get queued between requests until they are output. Here are the relevant functions bellow. They will produce the messages that you see at the top of your screen in PW admin:

$this->message("status message"); 
$this->message("status message that only appears in debug mode", Notice::debug); 
$this->error("error message"); 
$this->error("error message that only appears in debug mode", Notice::debug); 

If you are outside of a Wire derived object, you can call upon any API var to handle the notice for you. For example:

wire('session')->message('status message'); 
wire('pages')->error('error message'); 

Note that these reporting functions above are for the admin (and related modules), and they don't do anything on the front-end of your site. How you choose to debug or report errors on the front-end is at your discretion. Personally, I keep debug mode on in development, and this puts PHP into E_ALL | E_STRICT error reporting mode... it reports everything possible. If I need to examine the value of something on the front-end, I'll do it the old fashioned way with just PHP. Though others may prefer to go further with their debugging tools.

If you want to keep your own error log, here's how (anywhere in PW):

$log = new FileLog($config->paths->logs . 'my-log.txt'); 
$log->save('whatever message you want'); 

You can direct it to store logs wherever you want, but I suggest using the PW logs dir as shown in the example above. This will make the log appear in /site/assets/logs/, and this directory is not web accessible for security.

  • Like 12
Link to comment
Share on other sites

I wanted to add a note about exceptions on the front end. If you want to abort a request, throw a WireException:

throw new WireException('your error message');

That message will automatically go into the errors.txt log and email the site administrator. If the site is in debug mode or you are a superuser, it will report the error message to you as well. If not debug mode and not superuser, the user will get a generic "an error occurred, administrator has been notified" message.

Another exception you can throw is the 404. This will make PW display the 404 page (from your site tree) and send the proper 404 headers to the browser. PW doesn't interpret this as an error that needs to be logged or emailed.

throw new Wire404Exception('page not found');  

This is the exception you might want to throw if you get an unrecognized URL segment, for instance.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

u.answres ur.own questxion

uh... not totally sure how I answered my own question there :)

Sorry if I'm not too clear. In Ryan's answer, he has this:

$this->error("error message"); 
$this->error("error message that only appears in debug mode", Notice::debug); 

one has Notice::debug, the other doesn't. On testing those two, the first one displays a message in the admin, the other doesn't. i've checked site/assets/logs/errors.txt and it wasn't there. So, where is it?

Again, what exactly does Notice::debug do? and are there other flags that can be used?

Link to comment
Share on other sites

  • 5 months later...

Super useful info in here! I've created a new log file to catch all the spam using the method Ryan describes. Now, the file is getting big rather quickly (go figure)... Any way to limit the amount of logs that gets saved? I only need the last 100 to make sure everything is working correctly.

Link to comment
Share on other sites

Have a look in the /wire/core/FileLog.php file -- it contains a prune($bytes) method that you can use to reduce the size of the file. In your case, you may want to have a LazyCron hook prune the file to 100000 bytes or something near there. You could also just prune the file before or after your $log->save(). 

$log = new FileLog(wire('config')->paths->logs . 'mylog.txt'); 
$log->prune(100000);  
  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Anyway of checking if there is an error from a module.

i.e. I've added a hook to page save

$this->addHookAfter('Pages::save', $this, 'pageSaved');

and check for the publish button, but I would also like to check to see is the publish was successful or if there was any errors like missing fields for example.

Thanks

Link to comment
Share on other sites

1 hour ago, benbyf said:

Anyway of checking if there is an error from a module.

i.e. I've added a hook to page save


$this->addHookAfter('Pages::save', $this, 'pageSaved');

and check for the publish button, but I would also like to check to see is the publish was successful or if there was any errors like missing fields for example.

Thanks


I really don't understand what you are trying to achieve with the publish button and missing fields, but I use Tracy for all my module development debugging :)

Can you explain your needs a little more?

Link to comment
Share on other sites

2 minutes ago, benbyf said:

@adrian E.g. my module sends an email when a page is published, but I dont want it to send when the page contains errors.

OK, that doesn't really sound like a debug issue - I guess I am thrown off by the title of this thread.

I think your best bet is probably to hook into InputfieldForm::processInput and then I think you'll want to check for $event->checkErrors()

Sorry, not a very detailed / well checked answer, but I think that should get you going along the right path.

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...