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

docs: replace types in Logger #6638

Merged
merged 1 commit into from
Oct 11, 2022
Merged

docs: replace types in Logger #6638

merged 1 commit into from
Oct 11, 2022

Conversation

ddevsr
Copy link
Collaborator

@ddevsr ddevsr commented Oct 6, 2022

Description

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Comment on lines 89 to 91
$date = date('Y-m-d');
$expected = 'log-' . $date . '.php';
$logger->handle('info', 'This is a test log');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These codes are time-dependent.
If date('Y-m-d') is called on today 23:59:59.9999 and $logger->handle() is called on tomorrow 0:00:00,
This test fails.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, rare moment to get difference ms. If want real same time must give parameter to set name file manual.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now FileHandler::handle() is using date(). We should use Time instead of date().
Then we can stop time during test execution.

@@ -13,6 +13,7 @@

use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;
use Stringable;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stringable comes with PHP 8, we can't use it yet. 😢

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true. But a use statement won't error on an unknown class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP-CS-Fixer automate use statement if set string|\Stringable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as supporting PHP 7.4, we should not use Stringable.
It means a normal class Stringable and does not make sense.
See https://3v4l.org/TF3Ct

Comment on lines 102 to 174
public function testHandleWithInteger()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');
$expected = 'log-' . $date . '.log';
$logger->handle('info', 123456);

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> 123456\n";
$this->assertSame($expectedResult, $result);
}

public function testHandleWithArray()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');
$arrData = [
'firstName' => 'John',
'lastName' => 'Doe',
];
$expected = 'log-' . $date . '.log';
$logger->handle('info', print_r($arrData, true));

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> Array\n(
[firstName] => John
[lastName] => Doe\n)\n\n";
$this->assertSame($expectedResult, $result);
}

public function testHandleWithObject()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');

$obj = new stdClass();
$obj->firstName = 'John';
$obj->lastName = 'Doe';

$expected = 'log-' . $date . '.log';
$logger->handle('info', print_r($obj, true));

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> stdClass Object\n(
[firstName] => John
[lastName] => Doe\n)\n\n";
$this->assertSame($expectedResult, $result);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting of $message was from commit 4fcb760. However, I think this is in contradiction to this line:

* The message MUST be a string or object implementing __toString().

Also, this is not compliant with PSR-3 considering that the Logger is implementing Psr\Log\LoggerInterface.

All in all, I believe this behavior is a bug to begin with.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know that.

log_message('error', $this); // TypeError
$this->logger->error($this); // Okay. but PHPStan error
 ------ --------------------------------------------------------------------------------------------------------------------- 
  Line   app/Controllers/Home.php                                                                                             
 ------ --------------------------------------------------------------------------------------------------------------------- 
  9      Parameter #2 $message of function log_message expects string, $this(App\Controllers\Home) given.                     
  10     Parameter #1 $message of method Psr\Log\LoggerInterface::error() expects string, $this(App\Controllers\Home) given.  
 ------ --------------------------------------------------------------------------------------------------------------------- 

Also, this is not compliant with PSR-3 considering that the Logger is implementing Psr\Log\LoggerInterface.
All in all, I believe this behavior is a bug to begin with.

Yes, but fixing this may break existing apps.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenjis Before changes in this PR appear PHP 2 errors. With condition now PR, what about ignoring this 2 error phpstan?

Copy link
Member

@kenjis kenjis Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring PHPStan errors is very bad practice.
We should remove phpstan-baseline.neon.dist entries as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert.

system/Common.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to only change the PHPDoc types. Because this PR title is docs: replace types in Logger.
If you want to change any implementation, it is better to send another PR.

Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's minimize for now the changes in this PR to the docblock changes, otherwise those "other" changes would be a blocker for the merge.

Also, please squash the unrelated "trial-and-error" commits into the others. We don't want to show an unordered history of the file.

system/Log/Logger.php Outdated Show resolved Hide resolved
system/Log/Logger.php Outdated Show resolved Hide resolved
system/Test/Mock/MockLogger.php Outdated Show resolved Hide resolved
Comment on lines 102 to 174
public function testHandleWithInteger()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');
$expected = 'log-' . $date . '.log';
$logger->handle('info', 123456);

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> 123456\n";
$this->assertSame($expectedResult, $result);
}

public function testHandleWithArray()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');
$arrData = [
'firstName' => 'John',
'lastName' => 'Doe',
];
$expected = 'log-' . $date . '.log';
$logger->handle('info', print_r($arrData, true));

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> Array\n(
[firstName] => John
[lastName] => Doe\n)\n\n";
$this->assertSame($expectedResult, $result);
}

public function testHandleWithObject()
{
$config = new LoggerConfig();
$config->handlers[TestHandler::class]['path'] = $this->start;
$logger = new MockFileLogger($config->handlers[TestHandler::class]);

$logger->setDateFormat('Y-m-d');
$date = date('Y-m-d');

$obj = new stdClass();
$obj->firstName = 'John';
$obj->lastName = 'Doe';

$expected = 'log-' . $date . '.log';
$logger->handle('info', print_r($obj, true));

$fp = fopen($config->handlers[TestHandler::class]['path'] . $expected, 'rb');
$result = stream_get_contents($fp);
fclose($fp);

// did the log file get created?
$expectedResult = 'INFO - ' . $date . " --> stdClass Object\n(
[firstName] => John
[lastName] => Doe\n)\n\n";
$this->assertSame($expectedResult, $result);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert.

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash commits.

tests: increase coverage tests in Logger

use dedicated clean_path

refactor: implement __toString() in Logger

docs: replace param string message to phpdocblock

docs: replace param string message to phpdocblock

docs: replace types in Logger
Co-authored-by: kenjis <[email protected]>

docs: replace types in Logger

docs: replace types in Logger

revert refactoring in Logger
@ddevsr ddevsr removed the request for review from paulbalandan October 10, 2022 06:33
@ddevsr ddevsr requested a review from kenjis October 10, 2022 06:33
@ddevsr ddevsr requested a review from paulbalandan October 10, 2022 08:27
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ddevrs

@paulbalandan paulbalandan merged commit 2a8f8c7 into codeigniter4:develop Oct 11, 2022
@paulbalandan
Copy link
Member

Thank you, @ddevsr

@ddevsr ddevsr deleted the docs-types-logger branch October 11, 2022 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants