How to override a service I am trying to override a service by using a ServiceProvider Class

class MyModuleServiceProvider extends ServiceProviderBase
{
    public function alter(ContainerBuilder $container)
    {
        // Overrides MessageNotifier class to add loggin facilities to each message.
        $definition = $container-&gt;getDefinition('message_notify.sender');
        $definition-&gt;setClass('Drupal\my_module\MyModuleMessageNotifier')-&gt;addArgument([
            new Reference('logger.channel.message_notify')
        ]);
    }
}


This is out of topic, but FYI, the overrode service "message_notify.sender" is created by the message_notify module pertaining to the message stack modules

As you can see, I am adding an argument (a logger) to my new service

Here is the __construct() method of the parent (overrode) class:

  public function __construct(Manager $notifier_manager) {
    $this-&gt;notifierManager = $notifier_manager;
  }


Here is the __construct() method of my new class:

    public function __construct(Manager $notifier_manager, LoggerChannelInterface $logger)
    {
        parent::__construct($notifier_manager);
        $this-&gt;logger = $logger;
    }


But for an unknown reason the $logger variable is not an object but an array with one item... and this item is the LoggerChannel object.

Thus this code is not working but the following does:

    public function __construct(Manager $notifier_manager, array $logger)
    {
        parent::__construct($notifier_manager);
        $this-&gt;logger = $logger[0];
    }


What am I doing wrong?
The problem is that you are adding your argument as an array, so your __construct() method receives an array. So this:

$definition-&gt;setClass('Drupal\my_module\MyModuleMessageNotifier')-&gt;addArgument([
  new Reference('logger.channel.message_notify')
]);


Should be this:

$definition-&gt;setClass('Drupal\my_module\MyModuleMessageNotifier')
  -&gt;addArgument(new Reference('logger.channel.message_notify'));


(notice that the Reference is not wrapped in an array.