Kbi
Kbi

Reputation: 384

Symfony - log to multiple log files in a service

I'm trying to inject multiple monolog handler into a service. Right now my parent class injects a logger and the children class injects another logger. My goal is it to be able to log specific actions to specific log files.

My service.yaml:

App\Services\PrinterManager:
    arguments: ['@doctrine.orm.entity_manager','@logger', '', '', '', '','']
    tags:
        - { name: monolog.logger, channel: printerface}

App\Services\Printer\Printer:
    autowire: true
    autoconfigure: false
    public: false
    parent: App\Services\PrinterManager
    arguments:
        index_2: '@logger'
        index_3: '@oneup_flysystem.printer_invoice_filesystem'
        index_4: '@oneup_flysystem.printerface_content_filesystem'
        index_5: '@oneup_flysystem.sftp_filesystem'
        index_6: '@App\Services\PrinterApiService'
    tags:
        - { name: monolog.logger, channel: printerlog}

My monolog.yaml:

monolog:
  handlers:
    main:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
        channels: ["!event, !printerface", "!printerlog"]
    printerface:
        type: stream
        level: debug
        channels: ["printerface"]
        path: "%kernel.logs_dir%/printerface.log"
    printerlog:
        type: stream
        level: debug
        channels: ["printerlog"]
        path: "%kernel.logs_dir%/printerlog.log"

But it seems that the current service configuration breaks the constructor and I get the following error:

The argument must be an existing index or the name of a constructor's parameter.          

Is there any way to use two log files in a service?

Upvotes: 1

Views: 2236

Answers (2)

Alister Bulman
Alister Bulman

Reputation: 35139

I've not done it with a parent/child class, but with something a little simpler I'm using named parameters, this is what I have (with three different loggers):

# App/Subscribers/WebhookLoggingListener.php file
public function __construct(
    LoggerInterface $logger, 
    LoggerInterface $mailgunLog, 
    LoggerInterface $dripLog) {
}

# services.yml
App\Subscribers\WebhookLoggingListener:
    arguments:
        $logger: "@logger"
        $mailgunLog: "@monolog.logger.mailgun"
        $dripLog: "@monolog.logger.drip"
    tags:
       - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

If I was using the other loggers elsewhere I could also bind them to specific variable names:

services:
    _defaults:
        # ... other config
        bind:
            $dripLog: "@?monolog.logger.drip"

Upvotes: 1

amirmodi
amirmodi

Reputation: 312

This is the method that Symfony is using to replace parent's arguments in a child service:

/**
 * You should always use this method when overwriting existing arguments
 * of the parent definition.
 *
 * If you directly call setArguments() keep in mind that you must follow
 * certain conventions when you want to overwrite the arguments of the
 * parent definition, otherwise your arguments will only be appended.
 *
 * @param int|string $index
 * @param mixed      $value
 *
 * @return self the current instance
 *
 * @throws InvalidArgumentException when $index isn't an integer
 */
public function replaceArgument($index, $value)
{
    if (\is_int($index)) {
        $this->arguments['index_'.$index] = $value;
    } elseif (0 === strpos($index, '$')) {
        $this->arguments[$index] = $value;
    } else {
        throw new InvalidArgumentException('The argument must be an existing index or the name of a constructor\'s parameter.');
    }
    return $this;
}

As you can see, the indexes must be eighter same the argument variable names in the parent's constructor with a prefixed $ or an integer indicating the related argument.

So I think you must define your child service as below:

App\Services\Printer\Printer:
    autowire: true
    autoconfigure: false
    public: false
    parent: App\Services\PrinterManager
    arguments:
        2: '@logger'
        3: '@oneup_flysystem.printer_invoice_filesystem'
        4: '@oneup_flysystem.printerface_content_filesystem'
        5: '@oneup_flysystem.sftp_filesystem'
        6: '@App\Services\PrinterApiService'
    tags:
        - { name: monolog.logger, channel: printerlog}

Update:

After I reproduced your problem, I figured out that the solution is as below. With this solution, the Symfony autowiring will work for the child service.

App\Services\Printer\Printer:
    autowire: true
    autoconfigure: false
    public: false
    parent: App\Services\PrinterManager
    arguments:
        $arg2: '@logger'
        $arg3: '@oneup_flysystem.printer_invoice_filesystem'
        $arg4: '@oneup_flysystem.printerface_content_filesystem'
        $arg5: '@oneup_flysystem.sftp_filesystem'
        $arg6: '@App\Services\PrinterApiService'
    tags:
        - { name: monolog.logger, channel: printerlog}

$arg2, $arg3, $arg4, $arg5 and $arg6 must be replaced by your class constructor's argument names.

Upvotes: 0

Related Questions