Magento - how to write in the admin actions log?

Magento - how to write in the admin actions log?

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - how to write in the admin actions log?

Writing to the Magento Admin Actions Log

Hello developers! Today, we’re going to explore how to log admin actions in Magento. This is an essential aspect of maintaining security and auditing changes made by your e-commerce administrators.

Understanding the Admin Actions Log

Magento comes with a built-in feature that logs all critical admin activities. The admin_log table stores information about who did what, when they did it, and how severe the action was. However, Magento doesn’t provide an out-of-the-box way to log custom admin actions or to write to this table directly.

Writing Custom Logs

To add a new log entry, we need to create a custom module that extends the core functionality. Here’s a step-by-step guide:

  1. Create the config.xml file

In your custom module directory (for example, app/code/YourCompany/AdminActionsLog), create a config.xml file with the following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/config.xsd">
    <default>
        <!-- Add this node to enable the event observer -->
        <adminhtml>
            <events>
                <controller_action_predispatch_adminhtml_*>
                    <observers>
                        <your_module_log>
                            <type>singleton</type>
                            <class>YourCompany\AdminActionsLog\Observer\LogAdminAction</class>
                            <method>logAdminAction</method>
                        </your_module_log>
                    </observers>
                </controller_action_predispatch_adminhtml_*>
            </events>
        </adminhtml>
    </default>
</config>

Replace YourCompany with your actual vendor code.

  1. Create the Observer class

In the Observer directory under your module, create a file called LogAdminAction.php:

<?php

namespace YourCompany\AdminActionsLog\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\Http;

class LogAdminAction implements ObserverInterface
{
    protected $request;

    public function __construct(Http $request)
    {
        $this->request = $request;
    }

    public function execute(Observer $observer)
    {
        // Check if the current page is an admin page
        if (strpos($this->request->getPathInfo(), 'admin/') !== false) {
            // Insert your custom logic here to log the action
        }
    }
}
  1. Inserting a Log Entry

In the execute method, you can use Magento’s built-in logging functionality or even directly insert records into the admin_log table. Here’s an example of how to write a simple message to the error log:

$writer = new \Zend_Log_Writer_Stream(BP . '/log/admin.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('This is a custom admin action log entry.');

Remember, for more complex logging requirements or writing directly to the admin_log table, it’s better to use Magento’s resource model and commit transactions properly.

Using LaravelCompany Services

If you find yourself needing more advanced logging features or customizations that go beyond core Magento functionality, consider exploring our services at LaravelCompany.

We specialize in providing tailored solutions for e-commerce platforms like Magento. Whether it’s optimizing performance, implementing complex business logic, or integrating with third-party systems, our team of experienced developers can help you achieve your goals efficiently.

Conclusion

Writing to the admin actions log in Magento isn’t straightforward, but with a little bit of custom module development, you can ensure that all critical admin activities are tracked. Remember to always follow best practices for security and data management when handling sensitive information.

Don’t hesitate to reach out if you have any further questions or need assistance implementing more complex logging solutions. Happy coding!

Cache Database Security Extensions

Our network