← Back to PHP Course | Chapter 13: Error Handling | Lesson 4 of 5

PHP Error Logging

Error logging गलत होने वाली हर चीज़ की एक diary रखने जैसा है, जो सिर्फ आप देख सकते हैं वहाँ लिखी गई। Visitors एक friendly message देखते हैं जबकि real details आपके लिए save किए जाते हैं।
Syntax
php
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
<?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";
?>

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
<?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");
?>

Error Handlers के अंदर Logging

एक custom error handler को error_log() के साथ combine करने का मतलब है कि हर PHP-level error automatically एक log में लिख दी जाती है, आपकी codebase में हाथ से logging calls add किए बिना।

उदाहरण: Logging inside Error Handlers

php
<?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;
?>

Exception Logging

risky code को try-catch में wrap करना और exception का message व stack trace log करना users को scary technical detail से दूर रखता है साथ ही आपको बाद में failure diagnose करने के लिए ज़रूरी हर चीज़ देते हुए।

उदाहरण: Exception Logging

php
<?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.";
}
?>

Log File Management

बिना check किए बढ़ने वाली Log files searches को धीमा कर देती हैं और disk space भर सकती हैं; periodically file size check करना (या एक schedule पर logs rotate करना) debugging को तेज़ रखता है और storage issues रोकता है।

उदाहरण: Log File Management

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक log file में passwords या card numbers जैसा sensitive data log करना।
  2. एक ऐसी log file में लिखना जिसे web server user लिख नहीं सकता, ताकि कुछ भी log न हो।
  3. log_errors enable करना भूल जाना, ताकि errors बिल्कुल log में न लिखे जाएँ।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.