Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Logger\Adapter\Database

Hi,

I am trying to use the Phalcon database adapter to keep a history of all actions in a database.

My code doesn't work and I don't understand why.

In the file services.php, I have the following code:

$di->setShared('db', function () use ($config) {
  return new Mysql([
    'host' => $config->db->host,
    'username' => $config->db->username,
    'password' => $config->db->password,
    'dbname' => $config->db->dbname
  ]);
});

$di->setShared('logger', function () use ($config) {
  $db = new Mysql([
    'host' => $config->db->host,
    'username' => $config->db->username,
    'password' => $config->db->password,
    'dbname' => $config->db->dbname
  ]);
  $log = new DbLogger('errors', [
      'db'    => $db,
      'table' => 'logs'
  ]);
  return $log;
});

In my controller, I have the following code:

public function indexAction()
{
    $this->logger->info("test");
}

And the following table is used to store the logs:

CREATE TABLE `logs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `type` int(3) NOT NULL,
  `content` text,
  `created_at` int(18) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I have included all the adapters required (Phalcon\Logger\Adapter\Database, Phalcon\Db\Adapter\Pdo\Mysql, etc..).

Do you have an idea ?

why you create a new connection? reuse it

$di->setShared('db', function () use ($config) {
  return new Mysql([
    'host' => $config->db->host,
    'username' => $config->db->username,
    'password' => $config->db->password,
    'dbname' => $config->db->dbname
  ]);
});

$di->setShared('logger', function () use ($di) {
  $log = new DbLogger('errors', [
      'db'    => $di['db'],
      'table' => 'logs'
  ]);
  return $log;
});

try whit that or show us some error

Good luck



1.3k

Thank you for your answer.

I tried with your code but it still doesn't work.

I obtain the following error: Fatal error: Uncaught Error: Class 'Phalcon\Logger\Adapter\Database' not found

I am using the version 3.4.0 of Phalcon.



55.2k
Accepted
answer

Vanila phalcon has no Database logging adapter:

https://github.com/phalcon/cphalcon/tree/3.4.x/phalcon/logger/adapter

Im guessing you've seen it in an incubator project:

https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Logger/Adapter/Database.php

Either require the incubator project with composer, or just copy that one class into your sources (watch out for the namespace).



1.3k

Thank you Lajos Bencz.

I just added that one class into my sources.

So I put this file into "app/library/Phalcon/Logger/Adapter/".

Of course, I have added into the file loader.php the following code:

$loader->registerNamespaces([
  'Phalcon' => $config->app->libraryDir . '/Phalcon/'
]);

And this is the content of the file service.php:

use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Logger\Adapter\Database as DbLogger;

$di->setShared('db', function () use ($config) {
  return new Mysql([
    'host' => $config->db->host,
    'username' => $config->db->username,
    'password' => $config->db->password,
    'dbname' => $config->db->dbname,
    'charset' => $config->db->charset
  ]);
});

$di->setShared('logger', function () use ($di) {
  $logger = new DbLogger('errors', [
      'db'    => $di['db'],
      'table' => 'logs'
  ]);
  return $logger;
});