Friday, November 16, 2012

Using the error logs while developing in moodle/php

I developed a custom report for one of my moodle instances the past week.  I do enjoy developing and solving the programming problems that arise with working with a large amount of data.  A quick plug for stackoverflow.com, that is my programming mentor!  If I post a good clear question and show that I have performed my own due dillegence, I can get fantastic insights and help from the programming community that lives at stack overflow.

Here is one example of a question and response related to this moodle report development.

After deploying the report to production, I noticed the error log for the moodle instance was growing.  I opened the log and saw repeatedly that each time my report was requested it was causing an offset error in the log, like this:

[Fri Nov 16 09:37:37 2012] [error] [client 10.3.12.40] PHP Notice:  Undefined offset: 1 in C:\\wamp\\www\\rd\\report\\studentprogress\\dbfunctions.php on line 55, referer: http://10.2.28.7/rd/report/studentprogress/index.php

After a bit more due dillegence, I added a check for a null value around the offending code, like this:

    //if the struct is not empty, send back just the total time for each date
    if(! empty($struct) ){
        return $struct[$i]['totalweeklytime'];
    }else{
        return null;
    }


This was not a fatal error and did not show itself when viewing the report in a browser.  I should have been checking the error log more closely on my development instance before I deployed the plugin to production! On my development instance, i have errors reporting to the log file and not to the screen.  Check moodle Settings|Development|Debugging.  This is how i missed this.

Moral of the story?

Check your error logs even when you do not see issues in the browser.  In your development instance, either write errors to the screen or check the error logs while developing code!




2 comments:

  1. Funny, I looked at this post today which mentions checking the error logs with frequency. I am working on another piece which has been quietly writing an error over and over to the log file. It does not fail in the application and I am developing/testing on my live server, shame on me. If I was doing this work in my RD instance, where the debugging is set to the screen, and not the log file, I would have seen it sooner. Thanks and oops!

    ReplyDelete