Aşağıdaki örneği takip ederek symony log mesajlarını monolog ile veritabanına kaydedebilirsiniz. Verileri kaydetmek için yeni bir monolog channel doctrine_channel, handler doctrine ve bir servis application_country.utils.doctrine_handler yaratacağız.


Config_dev.yml


monolog:
channels: [doctrine_channel]
handlers:
main:
...
channels: [... !doctrine_channel]
console:
...
channels: [... !doctrine_channel]
doctrine:
type: service
channels: [doctrine_channel]
id: application_country.utils.doctrine_handler

Services.yml


services:
application_country.controller.country:
class: Application\CountryBundle\Controller\CountryController
arguments:
- "@monolog.logger.doctrine_channel"

application_country.utils.doctrine_handler:
class: Application\CountryBundle\Util\DoctrineHandler
arguments:
- "@doctrine.orm.entity_manager"

Log entity


namespace Application\CountryBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="log")
* @ORM\HasLifecycleCallbacks
*/
class Log
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\Column(name="message", type="text")
*/
private $message;

/**
* @ORM\Column(name="level", type="string", length=50)
*/
private $level;

/**
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;

/**
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->createdAt = new DateTime();
}

...
...
}

CountryController



namespace Application\CountryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
* @Route("", service="application_country.controller.country")
*/
class CountryController extends Controller
{
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* @Method({"GET"})
* @Route("")
*/
public function indexAction()
{
$this->logger->info('Welcome!');
$this->logger->warning('Go back!');
$this->logger->emergency('Help!');
}
}

DoctrineHandler


namespace Application\CountryBundle\Util;

use Application\CountryBundle\Entity\Log;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Handler\AbstractProcessingHandler;

class DoctrineHandler extends AbstractProcessingHandler
{
private $initialized;
private $entityManager;
private $channel = 'doctrine_channel';

public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct();

$this->entityManager = $entityManager;
}

protected function write(array $record)
{
if (!$this->initialized) {
$this->initialize();
}

if ($this->channel != $record['channel']) {
return;
}

$log = new Log();
$log->setMessage($record['message']);
$log->setLevel($record['level_name']);

$this->entityManager->persist($log);
$this->entityManager->flush();
}

private function initialize()
{
$this->initialized = true;
}
}

Test


Eğer http://country.dev/app_dev.php/ adresine giderseniz, veritabanında aşağıdaki bilgileri göreceksiniz.


mysql> SELECT * FROM log;
+----+----------+-----------+---------------------+
| id | message | level | created_at |
+----+----------+-----------+---------------------+
| 1 | Welcome! | INFO | 2016-11-29 22:26:07 |
| 2 | Go back! | WARNING | 2016-11-29 22:26:07 |
| 3 | Help! | EMERGENCY | 2016-11-29 22:26:07 |
+----+----------+-----------+---------------------+
3 rows in set (0.00 sec)

Okunacak notlar