I'd like to be able to differentiate the channel used by Monolog based on if a service is called by a Symfony command or by the application server.
For example:
class A {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
}
class B {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
}
class SomeCommand extends Symfony\Component\Console\Command\Command {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
protected function configure() {
$this->setName('my-app:command');
}
protected function execute(InputInterface $input, OutputInterface $output) {
// execute something from class A
// execute something from class B
}
}
If i run php bin\console my-app:command
I'd like Symfony to write everything to the commands
channel (file commands-{environment}.log), while if the class A and B are used by a request from a Controller, they write everything to another channel.
At the moment the config I have is:
monolog:
channels: ['commands']
handlers:
main:
type: rotating_file
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
channels: [!commands]
commands_only:
type: rotating_file
path: '%kernel.logs_dir%/commands-%kernel.environment%.log'
level: debug
channels: [commands]
Injecting the logger to the Command service only doesn't give me the result I'd want: that writes only log from the Command class to the file commands-{environment}.log, while the other classes keep logging to the file {envinronment}.log.
Thank you in advance for any help.