When updating PHP, modules, or templates (for example, ASPRO), errors may appear on the site that are not visible to users, but lead to page crashes or incorrect operation. To quickly identify such problems, it is convenient to enable logging of PHP errors with an indication of the specific page where they occur.
Why is this necessary?
- Find pages that are being dropped by search robots or users
- Catch PHP 8+ errors (
TypeError,Fatal error,Deprecated) - Understand which file and URL is causing the problem.
- It is safe to log errors without displaying them on the screen
Solution: logging PHP errors in header.php
The following is a minimal and secure code to insert to the very beginning of the file header.php, before any output and before connection /bitrix/header.php.
$__phpErrorLogFile = $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/php_errors_site.log';
if (!defined('LOG_PHP_ERRORS_ONCE')) {
define('LOG_PHP_ERRORS_ONCE', true);
@ini_set('display_errors', '0');
@ini_set('display_startup_errors', '0');
@ini_set('log_errors', '0');
@error_reporting(E_ALL);
$__w = function ($lvl, $msg, $file = '', $line = 0) use ($__phpErrorLogFile) {
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$url = ($scheme . '://' . ($_SERVER['HTTP_HOST'] ?? 'CLI') . ($_SERVER['REQUEST_URI'] ?? ''));
$dt = date('Y-m-d H:i:s');
$msg = str_replace(["\r", "\n"], [' ', ' '], (string)$msg);
@file_put_contents($__phpErrorLogFile, "[$dt] [$lvl] $msg | $file:$line | $url\n", FILE_APPEND | LOCK_EX);
};
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($__w) {
if (!(error_reporting() & $errno)) return false;
$__w('PHP', $errstr, $errfile, $errline);
return false;
});
set_exception_handler(function ($e) use ($__w) {
$__w('EXCEPTION', get_class($e) . ': ' . $e->getMessage(), $e->getFile(), $e->getLine());
});
register_shutdown_function(function () use ($__w) {
$e = error_get_last();
if ($e && in_array((int)$e['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR], true)) {
$__w('FATAL', $e['message'] ?? '', $e['file'] ?? '', (int)($e['line'] ?? 0));
}
});
}
What will be in the log
All errors will be recorded in a file.: /bitrix/php_interface/php_errors_site.log
Recording example:
[2026-01-08 14:32:11] [FATAL] Cannot access offset of type string on string | /bitrix/modules/aspro.max/classes/general/CMax.php:1467 | https://site.ru/contacts/
Important
- The code is intended for diagnostics. After checking the site, it is recommended to delete it.
- You should not keep constant logging on a combat site.
E_NOTICEandE_DEPRECATED. - It is ideal to use this code when the robot runs the site or after updates.
This approach allows you to quickly find problem areas in templates and modules without affecting users and SEO.