Php Error Logging: The Complete Developer's Guide [2025]
Published on April 7, 2025 by Dinesh Uprety
How to Log Errors in PHP: A Beginner’s Guide to Error Reporting and Debugging
Handling errors effectively is crucial for maintaining a stable and secure PHP application. Instead of displaying raw errors to users (which can expose sensitive information), you should log them for debugging while showing user-friendly messages.
Why Proper Error Handling Matters in PHP
Effective error logging is essential for:
- 🛡️ Enhanced security (hiding sensitive debug info from users)
- 🔍 Easier debugging (detailed error trails for developers)
- ⚡ Improved stability (graceful handling of runtime issues)
1. Configuring PHP Error Reporting
<?php // Development environment settings error_reporting(E_ALL); // Report all PHP errors ini_set('display_errors', 1); // Show errors on screen ini_set('log_errors', 1); // Enable error logging // Production environment settings /* error_reporting(E_ALL); ini_set('display_errors', 0); // Never show errors to users! ini_set('log_errors', 1); */?>
Key Configuration Options:
Setting Purpose Development Production
| Setting | Purpose | Development | Production |
|---|---|---|---|
error_reporting() |
Error level reporting | E_ALL |
E_ALL |
display_errors |
Show errors on screen | 1 (On) |
0 (Off) |
log_errors |
Enable error logging | 1 (On) |
1 (On) |
2. Creating a Custom Error Handler
<?php function customErrorHandler($errno, $errstr, $errfile, $errline) { $logMessage = sprintf( "[%s] Error %s: %s in %s on line %d%s", date('Y-m-d H:i:s'), $errno, $errstr, $errfile, $errline, PHP_EOL // New line for readability ); // Log to daily rotating file error_log($logMessage, 3, __DIR__.'/logs/errors_'.date('Y-m-d').'.log'); // Optional: Send email for critical errors if (in_array($errno, [E_ERROR, E_USER_ERROR])) { } } set_error_handler('customErrorHandler');?>
Handler Features:
- 📅 Daily log file rotation
- 📧 Critical error email alerts
- 🕒 Timestamped entries
- 📁 Secure file storage
3. Advanced Error Logging Techniques
Log File Organization
/logs/├── errors_2025-04-8.log├── fatal_errors.log└── archive/ ├── errors_2024-11-01.log └── ...
Security Best Practices
# .htaccess protection for log files<Files *.log> Order deny,allow Deny from all</Files>
4. Handling Fatal Errors
Custom error handlers cannot catch fatal errors (e.g., syntax errors). For those, use:
register_shutdown_function()to check for fatal errors.error_get_lastto retrieve the last error before shutdown.
<?php register_shutdown_function(function() { $error = error_get_last(); if ($error && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR])) { $message = sprintf( "[%s] FATAL ERROR (%s): %s in %s on line %d%s", date('Y-m-d H:i:s'), $error['type'], $error['message'], $error['file'], $error['line'], PHP_EOL ); error_log($message, 3, __DIR__.'/logs/fatal_errors.log'); } });?>
Fatal Errors Captured:
E_ERROR- Critical runtime errorsE_PARSE- Syntax parsing errorsE_CORE_ERROR- PHP startup failures
5. Production-Ready Implementation Checklist
- Configure
display_errors=0in production - Implement custom error handler
- Set up fatal error shutdown handler
- Create secure log directory structure
- Implement log rotation (daily/weekly)
- Set proper file permissions (chmod 600)
- Test error scenarios thoroughly
Conclusion
Professional PHP error handling involves:
- Proper error reporting configuration
- Custom error handlers with logging
- Secure log file management
- Fatal error capture mechanisms
Senior Software Engineer • Writer @ Laranepal • PHP, Laravel, Livewire, TailwindCSS & VueJS • CEO @ Laranepal & Founder @ laracodesnap