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.


Bizim monolog servisimiz


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

Monolog service girdisi


services:
football_frontend.logger.monolog:
class: Football\FrontendBundle\Service\MonologService
arguments: [@logger]
tags:
- { name: monolog.logger, channel: country }

Config.yml


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

Controller.yml


services:
football_frontend.controller.country:
class: Football\FrontendBundle\Controller\CountryController
arguments:
- @football_frontend.logger.monolog
- @logger

Controller


/**
* @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
}
}