PHP › Error & Exception Handling
What best practices should guide how an application logs, reports, and responds to exceptions differently across development and production environments?
AdvancedAnswer
In a development environment, it is generally helpful to display detailed exception messages and full stack traces directly to the developer to speed up debugging, while in production, detailed technical error information should never be shown directly to end users since it could reveal sensitive internal implementation details, and instead the application should log the full detailed error information to a secure logging system or monitoring service while showing users only a generic, friendly error message, striking the right balance between developer visibility and production security.
if (app()->environment('local')) {
// Show detailed error page with stack trace
} else {
// Log full details, show generic error message to user
Log::error($exception);
}Real-world example A company's production application logs full exception details, including stack traces, to a centralized error monitoring service while showing users only a friendly apology message, ensuring developers still have everything they need to diagnose issues without exposing sensitive information publicly.