I have created custom log handler in symfony. Creating it was not a problem, config: config/packages/dev/monolog.yaml
monolog:
handlers:
....
alerts:
type: service
id: App\Logger\AlertLogger
channels: [app]
level: warning
And my service:
<?php
namespace App\Logger;
use Monolog\Handler\AbstractProcessingHandler;
class AlertLogger extends AbstractProcessingHandler
{
protected function write(array $record)
{
// handle log
}
}
But the problem is that I want to send this logs to RabbitMQ and do something in Consumer, but there is no way to call any other Service. I tried to pass it to construct, but I am getting errors like:
Argument 3 passed to App\Logger\AlertLogger::__construct() must implement interface OldSound\RabbitMqBundle\RabbitMq\ProducerInterface, string given, called in .....
Thats because there is a construct definition in AbstractProcessingHandler.
=============== EDIT: My tests:
Parents class constructor:
public function __construct($level = Logger::DEBUG, $bubble = true)
{
$this->setLevel($level);
$this->bubble = $bubble;
}
I tried to add log handler as a service to services.yaml:
services:
.....
App\Logger\AlertLogger:
arguments:
$producer: OldSound\RabbitMqBundle\RabbitMq\Producer
And my log handler:
...
class AlertLogger extends AbstractProcessingHandler
{
/**
* @var ProducerInterface
*/
private $producer;
public function __construct(ProducerInterface $producer)
{
$this->producer = $producer;
}
......
But Im getting this error:
Argument 1 passed to App\Logger\AlertLogger::__construct() must implement interface OldSound\RabbitMqBundle\RabbitMq\ProducerInterface, string given, called in
When I try to set construct arguments:
....
class AlertLogger extends AbstractProcessingHandler
{
/**
* @var ProducerInterface
*/
private $producer;
public function __construct($level, $bubble, ProducerInterface $producer)
{
parent::__construct($level, $bubble);
$this->producer = $producer;
}
.....
I got error:
Cannot autowire service "App\Logger\AlertLogger": argument "$level" of method "__construct()" has no type-hint, you should configure its value explicitly.