Php Error Logging: The Complete Developer's Guide [2025]
Published on April 8, 2025 by Dinesh Uprety
![PHP Error Logging: The Complete Developer's Guide [2025]](https://laranepal.com/storage/post/thumbnail/01JRAD88JX5V4DXNXZDYDABHPR.png)
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_last
to 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 errors -
E_PARSE
- Syntax parsing errors -
E_CORE_ERROR
- PHP startup failures
5. Production-Ready Implementation Checklist
- Configure
display_errors=0
in 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