Logging in Magento 2

Complete guide to writing logs with examples

What is Logging in Magento 2?

Logging in Magento 2 is the process of recording system events, errors, and debug information into log files. It helps developers identify issues, debug errors, and monitor application behavior during development and production.

Why Logging is Important?

  • Helps in debugging errors
  • Tracks system activities
  • Useful in API and payment troubleshooting
  • Helps analyze deployment failures
  • Improves application monitoring

Default Log Files in Magento 2

  • var/log/system.log – General logs
  • var/log/exception.log – Exceptions and critical errors
  • var/log/debug.log – Debug logs (only when enabled)

Basic Example: Using LoggerInterface

Magento 2 uses Monolog and PSR Logger Interface for logging.


namespace Vendor\Module\Model;

use Psr\Log\LoggerInterface;

class Test
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute()
    {
        $this->logger->info("This is info log");
        $this->logger->error("This is error log");
        $this->logger->debug("Debug message");
    }
}

Log Levels in Magento 2

  • debug() – Detailed debugging information
  • info() – General information
  • notice() – Normal but significant events
  • warning() – Warning conditions
  • error() – Error conditions
  • critical() – Critical issues
  • alert() – Immediate action required
  • emergency() – System unusable

Enable Debug Logging

To enable debug logs, run the following command:


bin/magento setup:config:set --enable-debug-logging=true
bin/magento cache:flush

Create Custom Log File

Step 1: Create Custom Handler


namespace Vendor\Module\Logger;

use Magento\Framework\Logger\Handler\Base;
use Monolog\Logger;

class Handler extends Base
{
    protected $fileName = '/var/log/custom.log';
    protected $loggerType = Logger::DEBUG;
}

Step 2: Create Logger Class


namespace Vendor\Module\Logger;

class Logger extends \Monolog\Logger
{
}

Step 3: Configure di.xml


<type name="Vendor\Module\Logger\Logger">
    <arguments>
        <argument name="name" xsi:type="string">customLogger</argument>
        <argument name="handlers" xsi:type="array">
            <item name="system" xsi:type="object">Vendor\Module\Logger\Handler</item>
        </argument>
    </arguments>
</type>

Step 4: Use Custom Logger


use Vendor\Module\Logger\Logger;

class Test
{
    protected $logger;

    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }

    public function execute()
    {
        $this->logger->info("Custom log message");
    }
}

Quick Method (Not Recommended)

You can use Object Manager directly, but avoid this in production:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logger = $objectManager->get(\Psr\Log\LoggerInterface::class);

$logger->info("Quick log");

Logging in Adobe Commerce Cloud

In Adobe Commerce Cloud, logs may also be available in centralized logging:

  • var/log/system.log
  • var/log/cloud.log
  • magento-cloud log (CLI command)

Best Practices

  • Always use dependency injection
  • Create separate log files for custom modules
  • Use appropriate log levels
  • Avoid logging sensitive data
  • Disable debug logs in production

Conclusion

Logging in Magento 2 is essential for debugging and monitoring applications. By using LoggerInterface and custom log handlers, developers can efficiently track issues and maintain clean, structured logs.

Last updated: March 2026