10/07/2014 - SYMFONY
Aşağıdaki örnek log işlemini iki farklı şekilde yapar. Doğal olan
@logger
servis, mesajları "main" channelin dev.log dosyasına, bizim yarattığımız MonologService servis ise, mesajları "country" channelin county.log dosyasına yazar.namespace Football\FrontendBundle\Service;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
class MonologService
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function add($level, $message)
{
$this->logger->$level($message);
}
}
services:
football_frontend.logger.monolog:
class: Football\FrontendBundle\Service\MonologService
arguments: [@logger]
tags:
- { name: monolog.logger, channel: country }
monolog:
channels: ["country"]
handlers:
country:
type: stream
path: %kernel.logs_dir%/%kernel.environment%_country.log
channels: ["country"] # Only handle country channel logs
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!country"] # Handle all channel logs but country
services:
football_frontend.controller.country:
class: Football\FrontendBundle\Controller\CountryController
arguments:
- @football_frontend.logger.monolog
- @logger
/**
* @Route("/country", service="football_frontend.controller.country")
*/
class CountryController extends Controller
{
private $monologService;
private $logger;
public function __construct(
MonologService $monologService,
LoggerInterface $logger
) {
$this->monologService = $monologService;
$this->logger = $logger;
}
public function listAction()
{
$this->monologService->add('warning', 'message'); // This would use "country" channel
$this->logger->warning('message'); // This would use "main" channel
}
}