Comments (3)
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.
- 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
.
- what sql statements we want to log?
One may need to log only SELECT
queries, others only DELETE
s or a combination of sql statements.
- 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
SELECT
s > 3 seconds theninfo
notice
for everyDELETE
warning
forALTER 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.
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.
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)
- Set charset for result to utf8? HOT 2
- Adding multiple data with a single query HOT 3
- How to get raw before sql limit function? HOT 1
- Query "find and replace" HOT 5
- Feature request: possibility to use some complicated expression in select statement HOT 2
- Documentation needs update (Transactions page) HOT 1
- Get full raw SQL in 4.* before execution HOT 1
- Multiple conditions if empty records come HOT 4
- Transactions with rollback HOT 1
- Problem with MID HOT 3
- enum type HOT 2
- undefined method all
- Package does not install with PHP 8
- Timestamp field with default value on creation HOT 3
- php 8.0 support HOT 1
- Insert records in batch
- Is possible to use Postgresql Jsonb data type with Opis?
- Deprecation warning - PHP 8.1 HOT 8
- aggregate function in select
- Package maintenance?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from database.