Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Outdated DebugLoggerInterface::getLogs PHPDoc #47396

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

Closed
MatTheCat opened this issue Aug 26, 2022 · 5 comments
Closed

[HttpKernel] Outdated DebugLoggerInterface::getLogs PHPDoc #47396

MatTheCat opened this issue Aug 26, 2022 · 5 comments

Comments

@MatTheCat
Copy link
Contributor

Symfony version(s) affected

5.4, 6

Description

While trying to implement a DebugLoggerInterface around HttpKernel’s Logger I read the following:

* A log is an array with the following mandatory keys:
* timestamp, message, priority, and priorityName.
* It can also have an optional context key containing an array.

But it seems that since #42195 the LoggerDataCollector will expect the keys

  • channel
  • context
  • message
  • priority
  • priorityName
  • timestamp_rfc3339

So

  • timestamp is not needed
  • context is not optional
  • timestamp_rfc3339and channel are mandatory

It seems to me DebugLoggerInterface is almost exclusively used by the LoggerDataCollector so I guess the keys should match.

How to reproduce

Decorate HttpKernel’s logger to implement DebugLoggerInterface:

<?php

namespace App\Log;

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

#[AsDecorator('logger')]
class DebugLogger implements LoggerInterface, DebugLoggerInterface
{
    use LoggerTrait;
    
    private array $logsByRequest = [];
    
    public function __construct(
        private readonly LoggerInterface $logger,
        private readonly RequestStack $requestStack,
    ) {}

    public function log($level, $message, array $context = []): void
    {
        $this->logger->log($level, $message, $context);
        
        $request = $this->requestStack->getCurrentRequest();

        $this->logsByRequest[$request ? spl_object_hash($request) : ''][] = [
            'context' => $context,
            'message' => $message,
            'priority' => [
                LogLevel::DEBUG => 0,
                LogLevel::INFO => 1,
                LogLevel::NOTICE => 2,
                LogLevel::WARNING => 3,
                LogLevel::ERROR => 4,
                LogLevel::CRITICAL => 5,
                LogLevel::ALERT => 6,
                LogLevel::EMERGENCY => 7,
            ][$level],
            'priorityName' => $level,
            'timestamp' => time(),
        ];
    }

    public function getLogs(Request $request = null): array
    {
        if ($request) {
            return $this->logsByRequest[spl_object_hash($request)] ?? [];
        }
        
        return array_merge(...array_values($this->logsByRequest));
    }

    public function countErrors(Request $request = null): int
    {
        $errorCount = 0;
        foreach ($this->getLogs($request) as $log) {
            if ($log['priority'] >= 4) {
                ++$errorCount;
            }
        }
        return $errorCount;
    }

    public function clear()
    {
        $this->logsByRequest = [];
    }
}

then access the profiler’s logger panel. It will crash.

Possible Solution

Update mandatory keys to be used by the LoggerDataCollector.

Additional Context

No response

@stof
Copy link
Member

stof commented Aug 26, 2022

I guess we forgot to update the phpdoc of the interface at some point.

@MatTheCat
Copy link
Contributor Author

Okay I can submit a PR against 5.4 then 👍

@stof
Copy link
Member

stof commented Aug 26, 2022

@MatTheCat please do

@MatTheCat
Copy link
Contributor Author

Just saw timestamp is in fact used by the HtmlErrorRenderer (in the logs.html.php template). I will have to dig some more.

@MatTheCat
Copy link
Contributor Author

MatTheCat commented Aug 27, 2022

Hm at this point I have three questions about DebugLoggerInterface::getLogs expected return values:

  1. why is a key named timestamp_rfc3339? DateTimeInterface::RFC3339_EXTENDED is the only format including milliseconds, but it is not a timestamp (neither is DateTimeInterface::RFC3339 which is not used). Then the LoggerDataCollector stores it under a timestamp key 😵
  2. why was the timestamp key kept? It is redundant with timestamp_rfc3339.
  3. could DebugLoggerInterface::getLogs return POPOs instead of maps?

Poke @javiereguiluz 👋

@fabpot fabpot closed this as completed Sep 13, 2022
fabpot added a commit that referenced this issue Sep 13, 2022
… (MatTheCat)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[HttpKernel] Update DebugLoggerInterface::getLogs PHPDoc

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #47396
| License       | MIT
| Doc PR        | N/A

Commits
-------

ba39e60 [HttpKernel] Update DebugLoggerInterface::getLogs PHPDoc
fabpot added a commit that referenced this issue Oct 20, 2022
…e (MatTheCat)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[HttpKernel] Make Logger implement DebugLoggerInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

When starting a new project from a skeleton I was surprised not to see any log in the profiler and error pages. Turns out this depends of the logger implementing `DebugLoggerInterface` but AFAIK this only happen in Monolog’s bridge.

Given the API makes it weird to implement it in userland (see #47396) would it make sense to provide one by default?

Commits
-------

3be04ed [HttpKernel] Make Logger implement DebugLoggerInterface
symfony-splitter pushed a commit to symfony/http-kernel that referenced this issue Oct 20, 2022
…e (MatTheCat)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[HttpKernel] Make Logger implement DebugLoggerInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

When starting a new project from a skeleton I was surprised not to see any log in the profiler and error pages. Turns out this depends of the logger implementing `DebugLoggerInterface` but AFAIK this only happen in Monolog’s bridge.

Given the API makes it weird to implement it in userland (see symfony/symfony#47396) would it make sense to provide one by default?

Commits
-------

3be04eddaa [HttpKernel] Make Logger implement DebugLoggerInterface
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Oct 20, 2022
…e (MatTheCat)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[HttpKernel] Make Logger implement DebugLoggerInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

When starting a new project from a skeleton I was surprised not to see any log in the profiler and error pages. Turns out this depends of the logger implementing `DebugLoggerInterface` but AFAIK this only happen in Monolog’s bridge.

Given the API makes it weird to implement it in userland (see symfony/symfony#47396) would it make sense to provide one by default?

Commits
-------

3be04eddaa [HttpKernel] Make Logger implement DebugLoggerInterface
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this issue Jul 28, 2023
…e (MatTheCat)

This PR was squashed before being merged into the 6.2 branch.

Discussion
----------

[HttpKernel] Make Logger implement DebugLoggerInterface

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

When starting a new project from a skeleton I was surprised not to see any log in the profiler and error pages. Turns out this depends of the logger implementing `DebugLoggerInterface` but AFAIK this only happen in Monolog’s bridge.

Given the API makes it weird to implement it in userland (see symfony/symfony#47396) would it make sense to provide one by default?

Commits
-------

3be04eddaa [HttpKernel] Make Logger implement DebugLoggerInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants