How to debug / use the error log
Posted 28 March 2012 - 05:26 PM
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,
Posted 29 March 2012 - 08:28 AM
$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.
Posted 29 March 2012 - 08:39 AM
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.
Posted 21 May 2013 - 09:09 PM
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?
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users