Skip to content

Commit 2ce4b48

Browse files
authored
Merge pull request #6773 from paulbalandan/deprecation
Turn on logging for deprecation notices by default
2 parents a7de35a + c97227a commit 2ce4b48

File tree

9 files changed

+46
-24
lines changed

9 files changed

+46
-24
lines changed

app/Config/Exceptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ class Exceptions extends BaseConfig
6060
* Use this option to temporarily cease the warnings and instead log those.
6161
* This option also works for user deprecations.
6262
*/
63-
public bool $logDeprecationsOnly = false;
63+
public bool $logDeprecations = true;
6464

6565
/**
6666
* --------------------------------------------------------------------------
6767
* LOG LEVEL THRESHOLD FOR DEPRECATIONS
6868
* --------------------------------------------------------------------------
69-
* If `$logDeprecationsOnly` is set to `true`, this sets the log level
69+
* If `$logDeprecations` is set to `true`, this sets the log level
7070
* to which the deprecation will be logged. This should be one of the log
7171
* levels recognized by PSR-3.
7272
*

app/Config/Logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Logger extends BaseConfig
3838
*
3939
* @var array|int
4040
*/
41-
public $threshold = 4;
41+
public $threshold = (ENVIRONMENT === 'production') ? 4 : 9;
4242

4343
/**
4444
* --------------------------------------------------------------------------

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171

7272
<php>
7373
<server name="app.baseURL" value="http://example.com/"/>
74+
<server name="CODEIGNITER_SCREAM_DEPRECATIONS" value="1"/>
7475
<!-- Directory containing phpunit.xml -->
7576
<const name="HOMEPATH" value="./"/>
7677
<!-- Directory containing the Paths config file -->

system/Debug/Exceptions.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public function __construct(ExceptionsConfig $config, $request, ResponseInterfac
8383
if (! isset($this->config->sensitiveDataInTrace)) {
8484
$this->config->sensitiveDataInTrace = [];
8585
}
86-
if (! isset($this->config->logDeprecationsOnly, $this->config->deprecationLogLevel)) {
87-
$this->config->logDeprecationsOnly = false;
86+
if (! isset($this->config->logDeprecations, $this->config->deprecationLogLevel)) {
87+
$this->config->logDeprecations = false;
8888
$this->config->deprecationLogLevel = LogLevel::WARNING;
8989
}
9090
}
@@ -158,17 +158,20 @@ public function exceptionHandler(Throwable $exception)
158158
*/
159159
public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null)
160160
{
161-
if ($this->isDeprecationError($severity) && $this->config->logDeprecationsOnly) {
162-
return $this->handleDeprecationError($message, $file, $line);
163-
}
164-
165-
if (error_reporting() & $severity) {
161+
if ($this->isDeprecationError($severity)) {
166162
// @TODO Remove if Faker is fixed.
167-
if ($this->isFakerDeprecationError($severity, $message, $file, $line)) {
168-
// Ignore the error.
163+
if ($this->isFakerDeprecationError($message, $file, $line)) {
169164
return true;
170165
}
171166

167+
if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
168+
throw new ErrorException($message, 0, $severity, $file, $line);
169+
}
170+
171+
return $this->handleDeprecationError($message, $file, $line);
172+
}
173+
174+
if (error_reporting() & $severity) {
172175
throw new ErrorException($message, 0, $severity, $file, $line);
173176
}
174177

@@ -180,11 +183,10 @@ public function errorHandler(int $severity, string $message, ?string $file = nul
180183
*
181184
* @see https://github.com/FakerPHP/Faker/issues/479
182185
*/
183-
private function isFakerDeprecationError(int $severity, string $message, ?string $file = null, ?int $line = null)
186+
private function isFakerDeprecationError(string $message, ?string $file = null, ?int $line = null)
184187
{
185188
if (
186-
$severity === E_DEPRECATED
187-
&& strpos($file, VENDORPATH . 'fakerphp/faker/') !== false
189+
strpos($file, VENDORPATH . 'fakerphp/faker/') !== false
188190
&& $message === 'Use of "static" in callables is deprecated'
189191
) {
190192
log_message(

tests/system/Debug/ExceptionsTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ final class ExceptionsTest extends CIUnitTestCase
3131

3232
private \CodeIgniter\Debug\Exceptions $exception;
3333

34+
public static function setUpBeforeClass(): void
35+
{
36+
parent::setUpBeforeClass();
37+
38+
unset($_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS']);
39+
}
40+
41+
public static function tearDownAfterClass(): void
42+
{
43+
parent::tearDownAfterClass();
44+
45+
$_SERVER['CODEIGNITER_SCREAM_DEPRECATIONS'] = '1';
46+
}
47+
3448
protected function setUp(): void
3549
{
3650
parent::setUp();
@@ -45,7 +59,7 @@ public function testDeprecationsOnPhp81DoNotThrow(): void
4559
{
4660
$config = new ExceptionsConfig();
4761

48-
$config->logDeprecationsOnly = true;
62+
$config->logDeprecations = true;
4963
$config->deprecationLogLevel = 'error';
5064

5165
$this->exception = new Exceptions($config, Services::request(), Services::response());
@@ -69,7 +83,7 @@ public function testSuppressedDeprecationsAreLogged(): void
6983
{
7084
$config = new ExceptionsConfig();
7185

72-
$config->logDeprecationsOnly = true;
86+
$config->logDeprecations = true;
7387
$config->deprecationLogLevel = 'error';
7488

7589
$this->exception = new Exceptions($config, Services::request(), Services::response());

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ Helpers and Functions
223223
Error Handling
224224
==============
225225

226-
- You can now log deprecation errors instead of throwing them. See :ref:`logging_deprecation_errors` for details.
226+
- You can now log deprecation warnings instead of throwing exceptions. See :ref:`logging_deprecation_warnings` for details.
227+
- Logging of deprecations is turned on by default.
228+
- To *temporarily* enable throwing of deprecations, set the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value.
229+
- ``Config\Logger::$threshold`` is now, by default, environment-specific. For production environment, default threshold is still ``4`` but changed to ``9`` for other environments.
227230

228231
Others
229232
======
@@ -272,4 +275,3 @@ Bugs Fixed
272275
**********
273276

274277
- Fixed a bug when all types of ``Prepared Queries`` were returning a ``Result`` object instead of a bool value for write-type queries.
275-

user_guide_src/source/general/errors.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ Since v4.3.0, you can specify the exit code for your Exception class to implemen
134134

135135
When an exception implementing ``HasExitCodeInterface`` is caught by CodeIgniter's exception handler, the code returned from the ``getExitCode()`` method will become the exit code.
136136

137-
.. _logging_deprecation_errors:
137+
.. _logging_deprecation_warnings:
138138

139-
Logging Deprecation Errors
140-
==========================
139+
Logging Deprecation Warnings
140+
============================
141141

142142
.. versionadded:: 4.3.0
143143

@@ -161,3 +161,6 @@ After that, subsequent deprecations will be logged instead of thrown.
161161
This feature also works with user deprecations:
162162

163163
.. literalinclude:: errors/014.php
164+
165+
For testing your application you may want to always throw on deprecations. You may configure this by
166+
setting the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value.

user_guide_src/source/general/errors/012.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class Exceptions extends BaseConfig
99
{
1010
// ... other properties
1111

12-
public bool $logDeprecationsOnly = true;
12+
public bool $logDeprecations = true;
1313
public string $deprecationLogLevel = LogLevel::WARNING; // this should be one of the log levels supported by PSR-3
1414
}

user_guide_src/source/installation/upgrade_430.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ and it is recommended that you merge the updated versions with your application:
181181
* ``app/Config/DocTypes.php``
182182
* The property ``$html5`` to determine whether to remove the solidus (``/``) character for void HTML elements (e.g. ``<input>``) is added, and set to ``true`` by default for HTML5 compatibility.
183183
* ``app/Config/Exceptions.php``
184-
* Two additional public properties were added: ``$logDeprecationsOnly`` and ``$deprecationLogLevel``.
184+
* Two additional public properties were added: ``$logDeprecations`` and ``$deprecationLogLevel``.
185185
* ``app/Config/Routes.php``
186186
* 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.
187187

0 commit comments

Comments
 (0)