08.01.2026 Have you read it yet 1806 once It helped 1 to the reader

Logging PHP errors after updating the Bitrix kernel / switching to PHP8

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

  1. The code is intended for diagnostics. After checking the site, it is recommended to delete it.
  2. You should not keep constant logging on a combat site. E_NOTICE and E_DEPRECATED.
  3. 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.

Feedback

Was the material useful?

The assessment helps to understand which materials are worth developing and supplementing.

Discuss the task

Ask for help

Describe the task and leave your contacts. I will look at the introductory notes and contact you in a convenient way.

Direction *