How To Create Shopware 6 Custom Plugin Logger?

How To Create Shopware 6 Custom Plugin Logger

In Shopware 6, there is a plugin logger that allows your plugin to output logs to its own log file, separate from the default Shopware logs. This can be useful for debugging, auditing plugin-specific activity, or keeping logs organized.

Here is where you learn how to use a custom logger that logs to a special file. Every log file will append the environment and the date, both set in the.env file.

Step 1: Register the build-in LoggerFactory class.

Add the new <service> element in <services> of your custom plugin that is located in
ExamplePlugin/src/Resources/config/services.xml

<service id="company.plugin.logger" class="Monolog\Logger">
   <argument type="string">Company_PluginName</argument>
   <argument type="collection">
       <argument type="service" id="company.plugin.rotatingHandler"/>
   </argument>
</service>
<service id="company.plugin.rotatingHandler" class="Monolog\Handler\RotatingFileHandler">
     <argument type="string">%kernel.logs_dir%/company-name-%kernel.environment%.log</argument>
</service>

Let’s discuss about the class, id and argument that is defined in the service element.

  • class: The class is the actual PHP class Symfony’s dependency injection container will instantiate for that service.
  • id: "company.plugin.logger" is a variable identifier. This identifier will be used for dependency injection in your service.
  • Argument: It contains the file name as a string, which will be used as a prefix for logging.

Step 2: Inject the Logger service in your class.

Currently, in order to utilize the same in your custom class e.g CartValidator and log debugging data, you need to inject the logger service using its ID. I’ve used here logger service ID i.e company.plugin.logger which already I’ve declared.

<service id="StockBlocker\Core\Checkout\Cart\Custom\CustomCartValidator">
     <tag name="shopware.cart.validator"/>
     <argument type="service" id="company.plugin.logger"/>
</service>

Step 3: Usage of Logger in custom class.

You have passed the logger service as the first argument to your class constructor. You will need to define it in your constructor as an argument of the type LoggerInterface.

<?php
declare(strict_types=1);
namespace StockBlocker\Core\Checkout\Cart\Custom;
use Shopware\Core\Checkout\Cart\CartValidatorInterface;
use Psr\Log\LoggerInterface;

class CustomCartValidator implements CartValidatorInterface
{
  private LoggerInterface $logger;
  public function __construct(LoggerInterface $logger)
  {
    $this->logger = $logger;
  }
  public function validate(): void
  {

    $this->logger->info('Custom Cart Validator');
  }
}

Shopware 6 Custom Plugin Logger

The log file will be saved in the var/log directory. You can view the logs in that file.

Please contact us at manish@bay20.com or call us at +91-8800519180 / +91-9582784309 for any support related to shopware. You can also visit the Shopware development page to check the services we offer.