Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: Defining CRAFT_LOG_PHP_ERRORS as false still causes Craft to log PHP errors #12862

Closed
kbergha opened this issue Mar 10, 2023 · 2 comments
Assignees

Comments

@kbergha
Copy link

kbergha commented Mar 10, 2023

What happened?

Description

Defining CRAFT_LOG_PHP_ERRORS as false as described in https://craftcms.com/docs/4.x/config/#craft-log-php-errors still causes Craft to log PHP errors by overwriting error_log and log_errors from php.ini.

Steps to reproduce

  1. Have something like the following config in php.ini (this is for a Docker setup, running php:8.2.3-apache-bullseye, logging to stdout instead of to a file):
log_errors = On
error_log = /proc/self/fd/2
  1. In bootstrap.php, define CRAFT_LOG_PHP_ERRORS as false:
<?php
/**
 * Shared bootstrap file
 */

// Define path constants
define('CRAFT_BASE_PATH', __DIR__);
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH . '/vendor');

// https://craftcms.com/docs/4.x/config/#craft-log-php-errors
// Set to false to prevent PHP errors from being logged to storage/logs/phperrors.log
// Will be logged to whatever is set in php.ini (yay!)
define('CRAFT_LOG_PHP_ERRORS', false);

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH . '/autoload.php';

// Load dotenv?
if (class_exists(Dotenv\Dotenv::class)) {
    // By default, this will allow .env file values to override environment variables
    // with matching names. Use `createUnsafeImmutable` to disable this.
    Dotenv\Dotenv::createUnsafeMutable(CRAFT_BASE_PATH)->safeLoad();
}
  1. Trigger a PHP error, like a syntax error in config/general.php:
<?php

/**
 * General Configuration
 *
 * All of your system's general configuration settings go in here. You can see a
 * list of the available settings in vendor/craftcms/cms/src/config/GeneralConfig.php.
 *
 * @see \craft\config\GeneralConfig
 */

uppsie-daisy craft\config\GeneralConfig;
  1. Open your frontend in a browser.

Expected behavior

The error message should appear in what you've set up in php.ini.

For my setup, the Docker container logs should have gotten something like:

2023-03-10 13:26:14 { "Source":"Apache", "DateTime":"Fri Mar 10 12:26:14 2023", "Function" : "[php:error]" , "Process" : "[pid 33:tid , "Message" : "PHP Parse error: syntax error, unexpected namespaced name "craft\\config\\GeneralConfig" in /app/config/general.php on line 12" , "Referer" : "X-Forwarded-For":"172.18.0.1" }

Actual behavior

No error in what I've set up in php.ini (stdout).
Since I have not defined stream logging, storage/logs/phperrors.log is created and/or written to inside the container:

[10-Mar-2023 12:23:52 UTC] PHP Parse error: syntax error, unexpected namespaced name "craft\config\GeneralConfig" in /app/config/general.php on line 12

Cause of problem?

In craftcms/cms/bootstrap/bootstrap.php, the following code checks if it should set log_errors + error_log or not.

// Log errors to storage/logs/phperrors.log or php://stderr
if (!App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS')) {
    ini_set('log_errors', '1');

    if (App::isStreamLog()) {
        ini_set('error_log', 'php://stderr');
    } else {
        ini_set('error_log', $storagePath . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'phperrors.log');
    }
}

App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS') returns null if CRAFT_LOG_PHP_ERRORS is not defined.
App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS') returns false if CRAFT_LOG_PHP_ERRORS is defined as false.

Therefore, checking !App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS') will always return true, and Craft will overwrite log_errors + error_log.

Craft CMS version

4.4.0

PHP version

8.2.3

Operating system and version

Debian 11, Bullseye

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Amazon S3 - 2.0.1
Amazon SES - 2.0.1
Jason - 3.0.1
Json Client - dev-main
Odds & Ends - 4.1.2
Redactor - 3.0.3
Scout - 3.1.0

@kbergha kbergha changed the title [4.x]: Defining CRAFT_LOG_PHP_ERRORS as false as still causes Craft to log PHP errors [4.x]: Defining CRAFT_LOG_PHP_ERRORS as false still causes Craft to log PHP errors Mar 10, 2023
@i-just i-just self-assigned this Mar 21, 2023
@i-just
Copy link
Contributor

i-just commented Mar 21, 2023

Hi, thanks for reporting. I submitted a PR for this.

@brandonkelly
Copy link
Member

Craft 4.4.5 is out now with a fix for this, via #12940 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants