Effortlessly Log Plugin-Specific Data in Mautic

Logging is the unsung hero of application development, especially when debugging complex systems like Mautic. If you've ever struggled to sift through a sea of log entries in the main log file, you’re not alone. Fortunately, it’s easy to configure Mautic to log plugin-specific data to a separate file, streamlining your debugging process and keeping logs organized.

In this guide, I’ll show you how to achieve just that—step by step.

Why Use a Separate Log File?

When building custom Mautic plugins, it’s common to generate log entries specific to your plugin’s behavior. However, writing these logs to the main Mautic log file can quickly become overwhelming. Using a separate log file:

  • Reduces noise in the main log file.
  • Simplifies debugging for your plugin.
  • Helps isolate plugin-specific issues.

Now, let’s dive into the setup process.

Step 1: Locate and Copy the Existing Monolog Configuration

Mautic leverages Monolog for its logging capabilities. The default configuration for development mode resides in: <mautic-root>/app/config/config_dev.php.

To customize the logging behavior, copy this configuration to your local configuration file:
<mautic-root>/config/config_local.php.

Step 2: Add a Custom Logging Channel and Handler

Modify the Monolog configuration to add a new channel (e.g., plugindev) and its corresponding handler. Here’s how your updated configuration might look:

<?php

$container->loadFromExtension('monolog', [
    'channels' => [
        'mautic',
        'chrome',
        'plugindev', // New custom channel
    ],
    'handlers' => [
        'main' => [
            'formatter' => 'mautic.monolog.fulltrace.formatter',
            'type'      => 'rotating_file',
            'path'      => '%kernel.logs_dir%/%kernel.environment%.php',
            'level'     => 'debug',
            'channels'  => [
                '!mautic',
                '!plugindev',
            ],
            'max_files' => 7,
        ],
        'console' => [
            'type'   => 'console',
            'bubble' => false,
        ],
        'mautic' => [
            'formatter' => 'mautic.monolog.fulltrace.formatter',
            'type'      => 'rotating_file',
            'path'      => '%kernel.logs_dir%/mautic_%kernel.environment%.php',
            'level'     => 'debug',
            'channels'  => [
                'mautic',
            ],
            'max_files' => 7,
        ],
        'plugindev' => [ // Handler for the new channel
            'formatter' => 'mautic.monolog.fulltrace.formatter',
            'type'      => 'rotating_file',
            'path'      => '%kernel.logs_dir%/plugindev_%kernel.environment%.php',
            'level'     => 'debug',
            'channels'  => [
                'plugindev',
            ],
            'max_files' => 7,
        ],
        'chrome' => [
            'type'     => 'chromephp',
            'level'    => 'debug',
            'channels' => [
                'chrome',
            ],
        ],
    ],
]);

This configuration introduces a new plugindev channel that logs to plugindev_<environment>.php. This file will reside in the log directory specified by Mautic.

Step 3: Inject the Correct Logger into Your Plugin

With the logging configuration in place, the next step is to use the plugindev logger in your plugin. Here’s how you can achieve this by injecting the logger into a class.

<?php

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;


class MyTransportFactory extends AbstractTransportFactory
{
    public function __construct(
        private TransportCallback $transportCallback,
        EventDispatcherInterface $eventDispatcher,
        LoggerInterface $plugindevLogger // Inject the custom logger
    ) {
        parent::__construct(
            $transportCallback,
            $eventDispatcher,
            $plugindevLogger // Pass it to the parent for use
        );
    }

Step 4: Test Your Configuration

Once the configuration and injection are complete, test your implementation by triggering logs from your plugin. For instance, call info() on the logger to write a test message:

$this->plugindevLogger->info('Plugin-specific log entry.', ['context' => 'example']);

Wrapping Up

Creating a dedicated log file for your Mautic plugin is a simple yet effective way to keep your logs clean and your debugging process efficient. By leveraging Monolog’s flexible configuration and injecting the appropriate logger into your classes, you gain full control over your plugin's logging behavior.

Happy coding! 🎉