Giter VIP home page Giter VIP logo

Comments (3)

sorinsarca avatar sorinsarca commented on June 18, 2024

In practice, this isn't a good idea because there are a lot of use cases. We cannot just send all queries to the logger.

  1. what log level should we use?

LoggerInterface suggests to use info, but for some apps/websites a query that takes 3 seconds to complete can be viewed as a warning.

  1. what sql statements we want to log?

One may need to log only SELECT queries, others only DELETEs or a combination of sql statements.

  1. why to log all?

Some people just want to log a notice if the total time for all queries is greater than 2 seconds.
Or maybe they want to filter and group the queries, for example:

  • if total time of all SELECTs > 3 seconds then info
  • notice for every DELETE
  • warning for ALTER TABLE, DROP TABLE, etc.

As you can see, the best option is to handle the logging the way your app/project needs it.

Adding psr/log will not add something valuable to this library. A simple logging can be done in 5 lines of code.

use Psr\Log\LoggerInterface;
use Opis\Database\Connection;

function logSqlQueries(Connection $connection, LoggerInterface $logger) {
   foreach ($connection->getLog() as $item) {
        // here you can filter and choose the log level
        $logger->info($item['query'], ['time' => $item['time']]);
   }
}

If there is something I missed please let me know.

from database.

 avatar commented on June 18, 2024

So, its not simple. You may think about it some time or days. How to resolve the problem of the psr/log integration.

from database.

sorinsarca avatar sorinsarca commented on June 18, 2024

So, its not simple. You may think about it some time or days. How to resolve the problem of the psr/log integration.

It is simple: there is no problem to solve.

However, you can always extend the library with a few lines of code to add the logger.

<?php

use Opis\Database\Connection;
use Psr\Log\{
    LoggerAwareInterface,
    LoggerAwareTrait
};

class ConnectionWithLogger extends Connection implements LoggerAwareInterface
{
    use LoggerAwareTrait;

    /**
     * @inheritDoc
     */
    protected function execute(array $prepared)
    {
        $ret = parent::execute($prepared);

        if ($this->logQueries && $this->logger) {
            $last = end($this->log);
            $this->logger->info($last['query'], [
                'time' => $last['time'],
            ]);
        }

        return $ret;
    }
}

And use it

$conn = new ConnectionWithLogger(...);
$conn->setLogger(...);

$db = new \Opis\Database\Database($conn);
...

from database.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.