PHP Error Logging & Debugging ตอนที่1 : Basic


การแสดง Error ต่างๆนั้น เครื่องที่เป็น Production ควรจะตั้งค่าให้แสดง Error ทั้งหมด แต่ไม่ให้ออกไปที่ stdout (ออกทางหน้าจอ) แต่ควรกำหนดให้ไปออกที่ strerr แทน....

PHP Error Logging & Debugging ตอนที่1 : Basic

เรื่องการแสดง Error ต่างๆนั้น เครื่องที่เป็น Production ควรจะตั้งค่าให้แสดง Error ทั้งหมด แต่ไม่ให้ออกไปที่ stdout (ออกทางหน้าจอ) แต่ควรกำหนดให้ไปออกที่ strerr แทน (บันทึก message ต่างๆ เช่น error, warning, notice ลงไฟล์)

ก่อนจะทำอย่างนั้นมาดู directive ที่เกี่ยวข้องกันก่อน เอาเฉพาะที่สำคัญ ที่เหลือก็ดูได้จาก comment ใน php.ini
1. กำหนดประเภทของ error message ที่ต้องการ
2. กำหนดวิธีการแสดงผลหรือบันทึุก


NOTE: directive พวกนี้สามารถกำหนดได้ใน php.ini ทั้ง Windows และ Linux แต่สำหรับ Linux ยังสามารถกำหนดได้ที่ .htaccess ได้อีก เช่น

php_flag log_errors on
php_value error_log "/var/log/php_error.log"

 

1. กำหนดประเภทของ error message ที่ต้องการ
error_reporting จะกำหนดว่า จะแสดง error แบบใดออกมาบ้าง มีค่าต่างๆ ดังนี้ 

; E_ALL - All errors and warnings (doesn't include E_STRICT)
; E_ERROR - fatal run-time errors
; E_RECOVERABLE_ERROR - almost fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message

 

 2. กำหนดวิธีการแสดงผลหรือบันทึุก
มี directive ที่ต้องใช้ ดังนี้

display_errors = On | Off;ควรปิด สำหรับ Production
display_startup_errors = On | Off;ควรเปิด สำหรับ Debugging
log_errors = On | Off;ถ้าเปิดไว้ จะเป็นการบันทึก error ลง strerr

เมื่อเราได้กำหนด log_errors = On แล้ว ต่อมาเราต้องมากำหนด stderr ที่ต้องการ ให้กับ directive error_log

error_log = "/var/log/php_error.log"

เป็นการกำหนดไฟล์ที่ต้องการให้ PHP เก็บบันทึก error message

 error_log = syslog

 ค่า syslog เป็นการกำหนดให้ PHP เก็บบันทึก error message ไปยัง system log message
หมายความว่า ถ้าเป็น Windows PHP จะเก็บบันทึกที่ Event Log ดังภาพด้านล่าง

The image “http://xs218.xs.to/xs218/07322/Clipboard01.gif” cannot be displayed, because it contains errors.

 

จากภาพ เป็น PHP scritps ที่ทำงานผิดพลาด เนื่องจากเปิด socket ไปยัง website ที่ชื่อ ICT-chicken.com ไม่เจอ

ส่วน Linux ก็จะเก็บตามที่กำหนดไว้ใน syslog.conf (เกี่ยวกับบริการ syslogd)

ในขณะที่ PHP ทำงาน เราสามารถกำหนดค่าต่างๆได้ เช่นกำหนดรูปแบบของ error message เปิดหรือปิดการแสดง error
เราสามารถใช้ function error_log() และ sys_log() ในการสร้าง error message ออกมาเองตามต้องการได้อีกด้วย

พบปัญหา
หากว่าผมใช้ error_log = syslog แล้วเรียก PHP ผ่าน IIS ปรากฏว่า PHP ไม่ได้ไปบันทึกที่ event log ให้
และเมื่อทดลองใช้ error_log="/php_error.log" ปรากฏว่า PHP ทำงานตามปกติ ยังไม่ได้หาสาเหตุว่าทำไม ถ้าใครรู้ช่วยบอกที


ตอนแรกว่าจะเขียนบันทึกสั้นๆ ไว้ต่อตอนที่ีี่สองแล้วกันครับ

คำสำคัญ (Tags): #opensource#php#lamp#error#logging
หมายเลขบันทึก: 122732เขียนเมื่อ 28 สิงหาคม 2007 15:34 น. ()แก้ไขเมื่อ 22 พฤษภาคม 2012 14:42 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (1)

การใช้ syslog จะใช้กับ apache ได้หรือเปล่าครับ

น่าสนแฮะ เดี๋ยวผมลองที่เครื่องตัวเองบ้างดีกว่า Appserv for windows..

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท