From 618640f8014d7a841e8041004de9d8653d0c4cc8 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Oct 2022 11:57:04 +0800 Subject: [PATCH 1/6] Turn on logging for deprecations as default --- app/Config/Exceptions.php | 2 +- phpunit.xml.dist | 1 + system/Debug/Exceptions.php | 6 +++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Config/Exceptions.php b/app/Config/Exceptions.php index ca0713a33711..13a27f2239a0 100644 --- a/app/Config/Exceptions.php +++ b/app/Config/Exceptions.php @@ -60,7 +60,7 @@ class Exceptions extends BaseConfig * Use this option to temporarily cease the warnings and instead log those. * This option also works for user deprecations. */ - public bool $logDeprecationsOnly = false; + public bool $logDeprecationsOnly = true; /** * -------------------------------------------------------------------------- diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 741cd106e080..a0475ff0032e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -71,6 +71,7 @@ + diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index c8471cbf8eae..e55eb51565de 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -158,7 +158,11 @@ public function exceptionHandler(Throwable $exception) */ public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null) { - if ($this->isDeprecationError($severity) && $this->config->logDeprecationsOnly) { + if ($this->isDeprecationError($severity)) { + if (! $this->config->logDeprecationsOnly || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) { + throw new ErrorException($message, 0, $severity, $file, $line); + } + return $this->handleDeprecationError($message, $file, $line); } From 45cda740355d3ef07db39fb6fc06786c381966ca Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Oct 2022 12:01:08 +0800 Subject: [PATCH 2/6] Move faker deprecation check --- system/Debug/Exceptions.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index e55eb51565de..54e50d579dca 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -159,6 +159,11 @@ public function exceptionHandler(Throwable $exception) public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null) { if ($this->isDeprecationError($severity)) { + // @TODO Remove if Faker is fixed. + if ($this->isFakerDeprecationError($message, $file, $line)) { + return true; + } + if (! $this->config->logDeprecationsOnly || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) { throw new ErrorException($message, 0, $severity, $file, $line); } @@ -167,12 +172,6 @@ public function errorHandler(int $severity, string $message, ?string $file = nul } if (error_reporting() & $severity) { - // @TODO Remove if Faker is fixed. - if ($this->isFakerDeprecationError($severity, $message, $file, $line)) { - // Ignore the error. - return true; - } - throw new ErrorException($message, 0, $severity, $file, $line); } @@ -184,11 +183,10 @@ public function errorHandler(int $severity, string $message, ?string $file = nul * * @see https://github.com/FakerPHP/Faker/issues/479 */ - private function isFakerDeprecationError(int $severity, string $message, ?string $file = null, ?int $line = null) + private function isFakerDeprecationError(string $message, ?string $file = null, ?int $line = null) { if ( - $severity === E_DEPRECATED - && strpos($file, VENDORPATH . 'fakerphp/faker/') !== false + strpos($file, VENDORPATH . 'fakerphp/faker/') !== false && $message === 'Use of "static" in callables is deprecated' ) { log_message( From f94d834212986c07849c3905fe36c6f18d919553 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Oct 2022 12:13:28 +0800 Subject: [PATCH 3/6] Update documentations --- user_guide_src/source/changelogs/v4.3.0.rst | 4 +++- user_guide_src/source/general/errors.rst | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 8a18b5a8cc00..89f402a0a21e 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -221,7 +221,9 @@ Helpers and Functions Error Handling ============== -- You can now log deprecation errors instead of throwing them. See :ref:`logging_deprecation_errors` for details. +- You can now log deprecation warnings instead of throwing exceptions. See :ref:`logging_deprecation_warnings` for details. +- Logging of deprecations is turned on by default. +- To *temporarily* enable throwing of deprecations, set the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value. Others ====== diff --git a/user_guide_src/source/general/errors.rst b/user_guide_src/source/general/errors.rst index 152d6f74a7bc..ee3c09e8c64a 100644 --- a/user_guide_src/source/general/errors.rst +++ b/user_guide_src/source/general/errors.rst @@ -134,10 +134,10 @@ Since v4.3.0, you can specify the exit code for your Exception class to implemen When an exception implementing ``HasExitCodeInterface`` is caught by CodeIgniter's exception handler, the code returned from the ``getExitCode()`` method will become the exit code. -.. _logging_deprecation_errors: +.. _logging_deprecation_warnings: -Logging Deprecation Errors -========================== +Logging Deprecation Warnings +============================ .. versionadded:: 4.3.0 @@ -161,3 +161,6 @@ After that, subsequent deprecations will be logged instead of thrown. This feature also works with user deprecations: .. literalinclude:: errors/014.php + +For testing your application you may want to always throw on deprecations. You may configure this by +setting the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value. From 89c96f5eb3648d2e3cd4f55227712ab869701531 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Oct 2022 12:43:10 +0800 Subject: [PATCH 4/6] Adjust ExceptionsTest --- tests/system/Debug/ExceptionsTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index b34a8fddca7a..5c70bed57c01 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -31,6 +31,20 @@ final class ExceptionsTest extends CIUnitTestCase private \CodeIgniter\Debug\Exceptions $exception; + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + unset($_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS']); + } + + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + $_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS'] = '1'; + } + protected function setUp(): void { parent::setUp(); From 6716c5a649af418aabb3c47bf16fbe9703bd5ab9 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Fri, 28 Oct 2022 19:17:38 +0800 Subject: [PATCH 5/6] Adjust property name --- app/Config/Exceptions.php | 4 ++-- system/Debug/Exceptions.php | 6 +++--- tests/system/Debug/ExceptionsTest.php | 4 ++-- user_guide_src/source/general/errors/012.php | 2 +- user_guide_src/source/installation/upgrade_430.rst | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/Config/Exceptions.php b/app/Config/Exceptions.php index 13a27f2239a0..bf3a1b964aeb 100644 --- a/app/Config/Exceptions.php +++ b/app/Config/Exceptions.php @@ -60,13 +60,13 @@ class Exceptions extends BaseConfig * Use this option to temporarily cease the warnings and instead log those. * This option also works for user deprecations. */ - public bool $logDeprecationsOnly = true; + public bool $logDeprecations = true; /** * -------------------------------------------------------------------------- * LOG LEVEL THRESHOLD FOR DEPRECATIONS * -------------------------------------------------------------------------- - * If `$logDeprecationsOnly` is set to `true`, this sets the log level + * If `$logDeprecations` is set to `true`, this sets the log level * to which the deprecation will be logged. This should be one of the log * levels recognized by PSR-3. * diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index 54e50d579dca..830bdaafa097 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -83,8 +83,8 @@ public function __construct(ExceptionsConfig $config, $request, ResponseInterfac if (! isset($this->config->sensitiveDataInTrace)) { $this->config->sensitiveDataInTrace = []; } - if (! isset($this->config->logDeprecationsOnly, $this->config->deprecationLogLevel)) { - $this->config->logDeprecationsOnly = false; + if (! isset($this->config->logDeprecations, $this->config->deprecationLogLevel)) { + $this->config->logDeprecations = false; $this->config->deprecationLogLevel = LogLevel::WARNING; } } @@ -164,7 +164,7 @@ public function errorHandler(int $severity, string $message, ?string $file = nul return true; } - if (! $this->config->logDeprecationsOnly || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) { + if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) { throw new ErrorException($message, 0, $severity, $file, $line); } diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index 5c70bed57c01..7b9afdfa388d 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -59,7 +59,7 @@ public function testDeprecationsOnPhp81DoNotThrow(): void { $config = new ExceptionsConfig(); - $config->logDeprecationsOnly = true; + $config->logDeprecations = true; $config->deprecationLogLevel = 'error'; $this->exception = new Exceptions($config, Services::request(), Services::response()); @@ -83,7 +83,7 @@ public function testSuppressedDeprecationsAreLogged(): void { $config = new ExceptionsConfig(); - $config->logDeprecationsOnly = true; + $config->logDeprecations = true; $config->deprecationLogLevel = 'error'; $this->exception = new Exceptions($config, Services::request(), Services::response()); diff --git a/user_guide_src/source/general/errors/012.php b/user_guide_src/source/general/errors/012.php index bf70772b97e9..1b773613143c 100644 --- a/user_guide_src/source/general/errors/012.php +++ b/user_guide_src/source/general/errors/012.php @@ -9,6 +9,6 @@ class Exceptions extends BaseConfig { // ... other properties - public bool $logDeprecationsOnly = true; + public bool $logDeprecations = true; public string $deprecationLogLevel = LogLevel::WARNING; // this should be one of the log levels supported by PSR-3 } diff --git a/user_guide_src/source/installation/upgrade_430.rst b/user_guide_src/source/installation/upgrade_430.rst index 00f1e1c80f3a..c13a30ed6bdf 100644 --- a/user_guide_src/source/installation/upgrade_430.rst +++ b/user_guide_src/source/installation/upgrade_430.rst @@ -178,7 +178,7 @@ The following files received significant changes (including deprecations or visu and it is recommended that you merge the updated versions with your application: * ``app/Config/Exceptions.php`` - * Two additional public properties were added: ``$logDeprecationsOnly`` and ``$deprecationLogLevel``. + * Two additional public properties were added: ``$logDeprecations`` and ``$deprecationLogLevel``. * ``app/Config/Routes.php`` * Due to the fact that the approach to running Spark Commands has changed, there is no longer a need to load the internal routes of the framework. From c97227ac768a056ccea2328d76974c78f1e43bdb Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Tue, 1 Nov 2022 13:56:51 +0800 Subject: [PATCH 6/6] Make logger threshold to be environment-specific --- app/Config/Logger.php | 2 +- user_guide_src/source/changelogs/v4.3.0.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Config/Logger.php b/app/Config/Logger.php index eaf976b14b8b..695f5c827600 100644 --- a/app/Config/Logger.php +++ b/app/Config/Logger.php @@ -38,7 +38,7 @@ class Logger extends BaseConfig * * @var array|int */ - public $threshold = 4; + public $threshold = (ENVIRONMENT === 'production') ? 4 : 9; /** * -------------------------------------------------------------------------- diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 89f402a0a21e..20b8c8e154ee 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -224,6 +224,7 @@ Error Handling - You can now log deprecation warnings instead of throwing exceptions. See :ref:`logging_deprecation_warnings` for details. - Logging of deprecations is turned on by default. - To *temporarily* enable throwing of deprecations, set the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value. +- ``Config\Logger::$threshold`` is now, by default, environment-specific. For production environment, default threshold is still ``4`` but changed to ``9`` for other environments. Others ====== @@ -272,4 +273,3 @@ Bugs Fixed ********** - Fixed a bug when all types of ``Prepared Queries`` were returning a ``Result`` object instead of a bool value for write-type queries. -