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

Add SQL file logger #2911

Closed
wants to merge 1 commit into from
Closed

Add SQL file logger #2911

wants to merge 1 commit into from

Conversation

BenMorel
Copy link
Contributor

@BenMorel BenMorel commented Nov 8, 2017

I've been using a SQL file logger for years, that proved very helpful during development, are you interested to merge it?

It produces an output similar to:

SQL logger starting on Wed, 08 Nov 2017 16:30:05 +0000

Query 1: SELECT ... WHERE t0.id = ?
Parameters: 5
Types: 'integer'
This query: 0.001 seconds; total: 0.001 seconds.

Query 2: SELECT ... WHERE d0_.restaurantId = ? AND d0_.status IN (?)
Parameters: 5, ['active']
Types: 'integer', 102
This query: 0.001 seconds; total: 0.002 seconds.

Query 3: SELECT ... ST_Contains(z0_.boundary, ST_GeomFromWKB(?, 4326)) = 1 AND z0_.status <> ?
Parameters: Brick\Geo\Point(POINT (55.2238832 -21.0401197)), 'deleted'
Types: 'Point', 2
This query: 0.026 seconds; total: 0.028 seconds.

Note: ellipses are mine, the logger outputs the full query.

It writes a compact representation of parameters, representing objects as their class name along with their string representation if available, or their hash otherwise.

@lcobucci
Copy link
Member

@BenMorel thanks for your contribution. The logger can indeed be useful but I think not everyone might need it, so maybe you could be offered as a separate package. You can create a small lib that depends on DBAL and provides that SQLLogger implementation. What do you think?

@BenMorel
Copy link
Contributor Author

I already have it in a repo, that I'm trying to tidy up and split into specific repositories. It's just that the FileLogger does not really belong anywhere and would probably end up in a repo of its own, which would be overkill IMO.

Doctrine already ships with an EchoLogger, so I thought the FileLogger would belong there as well.

@morozov
Copy link
Member

morozov commented Nov 12, 2017

I agree with @lcobucci. Only the interface belongs to the package itself. All implementations (including the existing ones) don't. Otherwise, we may end up implementing all possible loggers here.

@Ocramius
Copy link
Member

Ocramius commented Nov 12, 2017 via email

@BenMorel
Copy link
Contributor Author

Okay... I thought that basic implementations (the ones that don't require extra packages or extensions) did belong to this package.

@BenMorel BenMorel closed this Nov 12, 2017
@greg0ire
Copy link
Member

greg0ire commented Dec 6, 2017

Quick implementation:

<?php

namespace Svod\Infrastructure\Persistence\Doctrine;

use Doctrine\DBAL\Logging\SQLLogger;
use Psr\Log\LoggerInterface;

final class PsrSQLLogger implements SQLLogger
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritDoc}
     */
    public function startQuery($sql, array $params = null, array $types = null)
    {
        $this->logger->debug(
            $sql,
            compact('params', 'types')
        );
    }

    /**
     * {@inheritDoc}
     */
    public function stopQuery()
    {
    }
}

Works for me ™️

morozov added a commit to morozov/dbal that referenced this pull request May 21, 2018
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in doctrine#2911 (comment)
morozov added a commit to morozov/dbal that referenced this pull request May 21, 2018
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in doctrine#2911 (comment)
morozov added a commit to morozov/dbal that referenced this pull request May 21, 2018
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in doctrine#2911 (comment)
morozov added a commit to morozov/dbal that referenced this pull request May 21, 2018
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in doctrine#2911 (comment)
morozov added a commit to morozov/dbal that referenced this pull request May 22, 2018
Additionally, deprecated `DebugStack` and `EchoSQLLogger` as per (my own) proposal in doctrine#2911 (comment)
morozov added a commit to morozov/dbal that referenced this pull request May 22, 2018
Additionally, deprecated `EchoSQLLogger` as per proposal in doctrine#2911 (comment)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants