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

Turn on logging for deprecation notices by default #6773

Merged
merged 6 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Config/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = false;
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.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Config/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Logger extends BaseConfig
*
* @var array|int
*/
public $threshold = 4;
public $threshold = (ENVIRONMENT === 'production') ? 4 : 9;

/**
* --------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

<php>
<server name="app.baseURL" value="http://example.com/"/>
<server name="CODEIGNITER_SCREAM_DEPRECATIONS" value="1"/>
<!-- Directory containing phpunit.xml -->
<const name="HOMEPATH" value="./"/>
<!-- Directory containing the Paths config file -->
Expand Down
26 changes: 14 additions & 12 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -158,17 +158,20 @@ 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) {
return $this->handleDeprecationError($message, $file, $line);
}

if (error_reporting() & $severity) {
if ($this->isDeprecationError($severity)) {
// @TODO Remove if Faker is fixed.
if ($this->isFakerDeprecationError($severity, $message, $file, $line)) {
// Ignore the error.
if ($this->isFakerDeprecationError($message, $file, $line)) {
return true;
}

if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
throw new ErrorException($message, 0, $severity, $file, $line);
}

return $this->handleDeprecationError($message, $file, $line);
}

if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $file, $line);
}

Expand All @@ -180,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(
Expand Down
18 changes: 16 additions & 2 deletions tests/system/Debug/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -45,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());
Expand All @@ -69,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());
Expand Down
6 changes: 4 additions & 2 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ 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.
- ``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
======
Expand Down Expand Up @@ -270,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.

9 changes: 6 additions & 3 deletions user_guide_src/source/general/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
2 changes: 1 addition & 1 deletion user_guide_src/source/general/errors/012.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion user_guide_src/source/installation/upgrade_430.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down