How can I write a custom function that will write error to my log file in Php?

How can I write a custom function that will write error to my log file in Php?

by Baiju Sharma -
Number of replies: 3

Below is my schedule task. I want to know is there a way I can log an error in my log.txt file.

Note: I don't want Moodle error logging function.

Want to know what step I will have to follow to do the same.

I just want a simple custom function which will write any error in my log.txt file. also, want to know where can I store this log.txt file as all my custom schedule tasks are in my task folder inside the certificate folder.(mod/certificate/task/scheduletask.php)


 public function execute() {      

         global $DB;  

         $subject = get_string('email_subject_course_expiry', 'mod_certificate','');

         $records = $DB->get_recordset_sql ("SELECT DISTINCT fullname AS usercoursename,usr.firstname AS firstname,usr.email AS useremail,usr.deviceid AS deviceid,

                                                FROM vw_complan_comptemp_comp 

                                                vw_user_devicedetails  AS usr

                                                ");

 if(is_null($records) || empty($records))

                 {                          exit;

                 }

              else

                 { 

foreach ($records as $id => $student) {

                $data = new \stdClass();                 

                    $data->usercoursename = $student->usercoursename;

                    $data->firstname = $student->firstname; 

                    $data->useremail= $student->useremail;

                    $data->deviceid= $student->deviceid;

                    $data->notifydate= $student->notifydate;

                  

                       if(time() ==  $data->notifydate)

                         {

                             $eol = PHP_EOL;

                             $header = "From:"."info@learntodrill.com". $eol;

                             $header .= "MIME-Version: 1.0". $eol;

                             $header .= "Return-Path:"."info@learntodrill.com". $eol;

                             $header .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;

                             

                                            $find[] = '[Course Name]';

                                            $replace[] = (string)($data->usercoursename);

                                            $find[] = '[Student First Name]';

                                            $replace[] = (string) ($data->firstname);

                                            $email_body = str_replace($find, $replace,  $emailbody);

                         

                              if (mail((string)($data->useremail), $subject, $email_body,  $header, '-f'."info@learntodrill.com"))

                                 {                                  

                                            $find[] = '[Course Name]';

                                            $replace[] = (string)($data->usercoursename);

                                            $msg_payload = str_replace($find, $replace, $msg_notify);  

                                 

                                   }  

                                     else 

                                { 

                                exit; 

                                     }

                       }

           }


Average of ratings: -
In reply to Baiju Sharma

Re: How can I write a custom function that will write error to my log file in Php?

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Are you asking how to write to a file in PHP? If so, see file_put_contents Yes

In reply to Paul Holden

Re: How can I write a custom function that will write error to my log file in Php?

by Baiju Sharma -

I have created below function to write error log, this function is not creating the file in the same directory. The error log files are getting written to the admin folder.I want this file to be in y current directory.

try {

                        $DB->insert_record('user_enrolments', $data);

                        } catch (\Exception $e) {

                           myErrorLog($e->getMessage());                        

                        }

-------------------------------------------------------------------------------

 function myErrorLog($message = 'no message') {     

        $myFile = "task_error.txt";

        $date = date('Y-m-d H:i:s')       

        if (file_exists($myFile)) {

           $fh = fopen($myFile, 'a');

           fwrite($fh, $date . ' ' . $message . "\n");

        } else {

           $fh = fopen($myFile, 'w');

           fwrite($fh, $date . ' ' . $message . "\n");

        }

        fclose($fh);   

    }

In reply to Paul Holden

Re: How can I write a custom function that will write error to my log file in Php?

by Baiju Sharma -

How to create error_log files in local folder of Moodle.I am getting permission denied error.