Troubleshooting

Log PHP errors for a specific file

Reviewed on 2 min read depuracion php

In production applications, error display must stay disabled so that paths, queries and other internal data are not revealed to visitors.

Preferably debug in a staging environment that reproduces the problem. If you exceptionally need to diagnose a file in production and PHP gets as far as running it, keep display_errors disabled and send errors to a private log outside public_html:

error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', '1');
ini_set('error_log', '/home/CPANEL_USUARIO/logs/php-debug.log');

error_reporting defines which errors will be logged and E_ALL applies the maximum level.

These directives apply at run time. They cannot log a syntax or compile error in the same file if that error stops PHP from reaching them. In that case, first enable logging for the effective PHP version from cPanel, or use a valid helper file that applies these directives and then includes the file you want to check.

Before testing, create the directory outside the web root and protect the file with 0600 permissions. Limit the capture to the time strictly needed and avoid logging sensitive data.

When you finish, remove these lines from the file, check that display_errors is still disabled and delete the log once it is no longer needed for the diagnosis.

The real scope of these lines

Even if you place them in a specific file, the settings affect the rest of that PHP execution, including files loaded afterwards; they do not filter the log so that only that file's errors appear. If you need to narrow the test, use a controlled request and filter its entries by time and path.

Add the code in a valid PHP position: a declare(strict_types=1) statement or a namespace may have position requirements. Do not automatically prepend instructions to any file. Validate the syntax with the same PHP version before reproducing the error.

You may also find useful

Something wrong or out of date? Tell us and we will review it.