โค้ด: เลือกทั้งหมด
$fp = fopen(‘/var/log/mylog.txt’, ‘a’);
fwrite($fp, ‘20121207:some text’);
fclose($fp);
นอกจากนั้น ยังสามารถเลือกให้เป็นการส่ง email แทนการเขียน log ลงไฟล์ก็ได้
การใช้งาน
โค้ด: เลือกทั้งหมด
// หากคุณไม่มี FOO ให้ส่งอีเมลไปยังผู้ดูแลระบบ
if(! ($ foo = allocate_new_foo ())) {
error_log("We are out of FOOs!", 1, "[email protected]");
}
โค้ด: เลือกทั้งหมด
error_log(message, message_type, destination, options)
message_type: ใช้ค่า 0,1,3,4 ซึ่งมีความหมายดังนี้
0: เป็นค่า Default ซึ่งเขียนลงไปใน System Log (ตามที่ระบุในค่า error_log ของ PHP ในไฟล์ php.ini)
1: เลือกให้ส่ง message ในรูปแบบ email ไปยัง destination โดยใช้ Header ของ email ตาม options
3: เลือกให้เขียน message ลงในไฟล์ที่กำหนดใน destination (ไม่ใช่ system log) โดยจะต้องเขียน newline ต่อท้ายบรรทัดเอง
4: เลือกให้เขียนลงไปใน SAPI (จากการทดสอบบน apache บน Ubuntu พบว่า เขียนลงที่เดียวกับ System
destination: หาก message_type=1 ให้ระบบ email address ที่ต้องการส่งถึง, หาก message_type=3 ให้ระบุตำแหน่งของไฟล์ที่ต้องการ
options: ในกรณี message_type=1 สามารถระบุ header ของ email ได้
สรุป Erorr log () คือ ตามโฮสหรือเซิพเวอร์ต่าง ๆ จะตั้งค่าให้มีการเก็บ log ไว้ครับ จะแยก log ทำงานปกติกับ error log
error เกิดขึ้นได้หลายกรณี ตั้งแต่การเขียน php ผิดหลัก ใส่ path ผิด คืออะไรที่มันผิดปกติมันจะถูกเก็บไว้ใน error log
อ้างอิง
1 https://www.w3schools.com/php/func_error_log.asp
2 https://www.php.net/manual/en/function.error-log.php
3 https://sysadmin.psu.ac.th/2012/12/07/using-php-error_log/