1

I am working on Moodle project.I want to is there a way I can write my error into Log.txt file in PHP.

Below is my code

class drill_auto_enrolluser extends \core\task\scheduled_task{

public function execute() {

    global $DB;
    $name="";
    $description="";
    $descriptionformat ="";
    $userid="";
    $templateid="";
    $timecreated="";
    $timemodified=0;
    $origtemplateid=NULL;
    $status=0;
    $duedate=0;
    $reviewerid=NULL;     

         $DATA = $DB->get_recordset_sql ("Select name,description,descriptionformat,userid,templateid from vw_new_user_lp");

            foreach ($DATA as $id => $rec) {

                 $record = new \stdClass();                                          
                             $record ->name = $rec->name;
                             $record ->description= $rec->description;
                             $record ->descriptionformat=$rec->descriptionformat;
                             $record ->userid= $rec->userid;    
                             $record->origtemplateid=$origtemplateid;
                             $record ->templateid=$rec->templateid;
                             $record->status= $status;
                             $record->duedate= $duedate;
                             $record->reviewerid= $reviewerid;
                             $record->timecreated= time();
                             $record->timemodified = $timemodified;
                             $record->usermodified = $userid;

                $DB->insert_record('competency', $record);

      }

}

}

Baiju Sharma
  • 115
  • 7

1 Answers1

0

In this case you must know how the error is reported. This is usually done either with an exception, or via a return value. In the first case, if the exception is not caught, the application aborts (which is as it should be).

For example, supposing that the insert throws an exception of class MoodleDatabaseException (of course, you need to check what exception is actually thrown):

try {
    $DB->insert_record('competency', $record);
} catch (\MoodleDatabaseException $err) {
    myErrorLog($err->getMessage());
}

Otherwise, if the function returns for example true or false, you would go:

if (true !== $DB->insert_record('competency', $record)) {
    myErrorLog($DB->functionThatReturnsLastErrorMessage());
}

In both cases you need a myErrorLog function. I think that Moodle has a logging facility and you may want to check it out, and possibly ask a more detailed follow-up question. You might also decide to use the Apache error_log facility depending on what you need. In a pinch, you can use a text log file:

function myErrorLog($message = 'no message') {
    $date = date('Y-m-d H:i:s');
    $fp   = fopen('/var/log/moodle-errors.log', 'a');
    if (!$fp) {
         throw new \Exception("Could not open log file! Permission error?");
    }
    fwrite($fp, $date . ' ' . $message . "\n");
    fclose($fp);
}

The above code does not worry about multi-user locking, which can be problematic. This is good for a quick and dirty debug; otherwise you need to use whatever functions are supplied (or look to integrating Log4Php or Monolog, maybe; but this looks like a demanding project).

Update

As I haven't been too clear (English is not my mother language), you're left with the problem of where to define the myErrorLog function so as to make it available everywhere, but never have it defined twice to avoid errors.

For a quick debugging you can replace myErrorLog with error_log which will output to the Apache error log file, thus also avoiding both concurrency issues and permission problems.

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • Thanks for the reply, I don't want to use Modle error logging function.I will try yours custom function.If any doubt will comment here. – Baiju Sharma Jun 09 '17 at 14:00
  • Thanks, LSemi..Your English words are easy to understand.I am facing permission problem.Warning: fopen(/moodle_error.txt): failed to open stream: Permission denied in /home/onlin200/public_html/mod/certificate/classes/task/errorlogging_function.php on line 13 Scheduled task failed: AutoEnroll user in competency courses (mod_certificate\task\drill_auto_enrolluser ),Could not open log file! Permission error? – Baiju Sharma Jun 15 '17 at 16:35
  • You are writing to /, and the web server does not have permission. Try /tmp/moodle-errors.txt or add a dot before the path: ./moodle_error.txt . – LSerni Jun 15 '17 at 16:38
  • I have set 777 permission to all the necessary PHP file.I will try your suggested solution.Thanks. – Baiju Sharma Jun 15 '17 at 16:48
  • I have tried the above solution, but not working for me.No Luck.What to do now? – Baiju Sharma Jun 15 '17 at 16:53
  • have you tried with the error_log function? that would write to the apache error log. – LSerni Jun 15 '17 at 17:01
  • With myErrorLog function, errorlog files are getting created in the admin folder.It not getting created in my current directory. – Baiju Sharma Jun 15 '17 at 17:29
  • Try specifying the full path of the directory where the errorlog has to be created. Remember that the Apache user needs to have traverse permission on all path components, and write permission on the destination directory. I'm sorry, I don't think I can help you further. Note: 777 permission on PHP files has nothing to do with the permissions on the target directory. Actually, PHP file permissions are best left at 644 (or 640 if your configuration allows, and you're a bit paranoid). 755 for the directories. – LSerni Jun 15 '17 at 18:08
  • I have added fullpath, now my error_log files are getting created inside required folder.Thanks. – Baiju Sharma Jun 21 '17 at 15:47
  • You're welcome. If you feel the answer is correct you may accept it, or you can point out if something needs to be added to make it more useful for anyone who might come across it in the future. – LSerni Jun 21 '17 at 15:49