We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



4.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.



77.7k
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).



4.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;
});

I am having 2 entries for each hit. What can be that i am doing wrong?



92
edited Mar '20

Can anyone tell me how to insert text it to type column or another field. I would like to add another field or change type to custom values. Reason:For easier search query. Suppose I want to search for specific order status or Order id. How this is possible

Can anyone tell me how to insert text it to type column or another field. I would like to add another field or change type to custom values. Reason:For easier search query. Suppose I want to search for specific order status or Order id. How this is possible

You need to adapt INSERT query in method logInternal() as you need. https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Logger/Adapter/Database.php#L123