catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Feature request: global error logging #468

Closed KirDE closed 3 years ago

KirDE commented 8 years ago

one flag to enable logging (into file) of all database errors.

kanuj commented 7 years ago

Since this can be handled at the database configuration level, I think there's no need to bloat the database interaction layer. Keeping it in the database config allows you more flexibility, such as the option to log it to a file/table etc.

KirDE commented 7 years ago

you misunderstoo d the request. it is about logging invalid / failed queries. MySQL doesn't log invalid/failed queries anywhere. http://stackoverflow.com/questions/4631133/mysql-log-of-invalid-queries

turbopixel commented 7 years ago

We create a simple medoo wrapper include monolog logging. Create a class named "Database", extends Medoo and overwrite methods like:

  public function insert($table, $data) {
    $this->logger->info( 'insert', ["table" => $table, "data" => $data] );
    return parent::insert($table, $data);
  }

So for error logging, I write a errorHandler: Query/Exec method:

  public function query($query, $context = []) {
    $this->logger->info('query', $context);
    $result = parent::query($query);
    $this->handleSqlError( $result ); // <-- handle errors
    return $result;
  }

Error handling:

  private function handleSqlError($query) {
    $error = $this->error();

    if ($error[0] != '00000') {
      $this->logger->critical('ERROR', ["sql" => $query,"error" => $error]);
    }

  }