PHP Error Logging
In this page:
error_log("message"); // server log
error_log("message", 3, "path/to/file.log"); // custom file
set_error_handler("handler_function");
Error Logging का Introduction
Visitors को raw errors दिखाना implementation details leak करता है और unprofessional दिखता है; error_log() इसके बजाय error messages server की log file में लिखता है, जहाँ developers users को कुछ भी expose किए बिना उन्हें review कर सकते हैं।
उदाहरण: Introduction to Error Logging
<?php
// Call `error_log("A test error message from the application")`
error_log("A test error message from the application");
// Print "Error written to the server log, not shown to visitors" to the output
echo "Error written to the server log, not shown to visitors";
?>
Login to try C/C++/Java/PHP code in the editor
Custom Files में Logging
error_log() को message type की तरह 3 pass करना, एक file path के साथ, उस specific message को PHP के default system log के बजाय आपकी अपनी custom log file में redirect करता है, जो application logs को organized रखने में मदद करता है।
उदाहरण: Logging to Custom Files
<?php
// Call `error_log("Custom log entry" . PHP_EOL, 3, "app_errors.log")`
error_log("Custom log entry" . PHP_EOL, 3, "app_errors.log");
// Print `file_get_contents("app_errors.log")` to the output
echo file_get_contents("app_errors.log");
?>
Login to try C/C++/Java/PHP code in the editor
Error Handlers के अंदर Logging
एक custom error handler को error_log() के साथ combine करने का मतलब है कि हर PHP-level error automatically एक log में लिख दी जाती है, आपकी codebase में हाथ से logging calls add किए बिना।
उदाहरण: Logging inside Error Handlers
<?php
set_error_handler(function ($errno, $errstr) {
// Call `error_log("Handled error: $errstr")`
error_log("Handled error: $errstr");
// Print "Logged automatically" to the output
echo "Logged automatically";
});
// Print `$undefinedVar` to the output
echo $undefinedVar;
?>
Login to try C/C++/Java/PHP code in the editor
Exception Logging
risky code को try-catch में wrap करना और exception का message व stack trace log करना users को scary technical detail से दूर रखता है साथ ही आपको बाद में failure diagnose करने के लिए ज़रूरी हर चीज़ देते हुए।
उदाहरण: Exception Logging
<?php
// Try running this block; jump to `catch` if it throws
try {
// Throw a new `Exception` with message "Payment failed"
throw new Exception("Payment failed");
// Catch Exception $e
} catch (Exception $e) {
// Call `error_log($e->getMessage() . "\n" . $e->getTraceAsString())`
error_log($e->getMessage() . "\n" . $e->getTraceAsString());
// Print "Something went wrong. Please try again." to the output
echo "Something went wrong. Please try again.";
}
?>
Login to try C/C++/Java/PHP code in the editor
Log File Management
बिना check किए बढ़ने वाली Log files searches को धीमा कर देती हैं और disk space भर सकती हैं; periodically file size check करना (या एक schedule पर logs rotate करना) debugging को तेज़ रखता है और storage issues रोकता है।
उदाहरण: Log File Management
<?php
// Call `file_put_contents("app_errors.log", "Sample log line\n", FILE_APPEND)`
file_put_contents("app_errors.log", "Sample log line\n", FILE_APPEND);
// Declare `$size`, set to `filesize("app_errors.log")`
$size = filesize("app_errors.log");
// Print "Log file size: $size bytes" to the output
echo "Log file size: $size bytes";
?>
Login to try C/C++/Java/PHP code in the editor
- एक log file में passwords या card numbers जैसा sensitive data log करना।
- एक ऐसी log file में लिखना जिसे web server user लिख नहीं सकता, ताकि कुछ भी log न हो।
log_errorsenable करना भूल जाना, ताकि errors बिल्कुल log में न लिखे जाएँ।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: