You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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();
}
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;
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.
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
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 overwritingerror_log
andlog_errors
from php.ini.Steps to reproduce
php:8.2.3-apache-bullseye
, logging to stdout instead of to a file):bootstrap.php
, defineCRAFT_LOG_PHP_ERRORS
asfalse
:config/general.php
: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 setlog_errors
+error_log
or not.App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS')
returnsnull
if CRAFT_LOG_PHP_ERRORS is not defined.App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS')
returnsfalse
if CRAFT_LOG_PHP_ERRORS is defined asfalse
.Therefore, checking
!App::parseBooleanEnv('$CRAFT_LOG_PHP_ERRORS')
will always returntrue
, and Craft will overwritelog_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
The text was updated successfully, but these errors were encountered: