🛠️ Magento 2 Logging & Debugging– Trace Issues Like a Pro
Something went wrong? Don’t panic! Magento 2 comes with powerful logging and debugging tools to help you find and fix issues fast — like a real coding detective. 🕵️♂️ Let’s walk through the tools you need.
📝 Magento 2 Log Files
Magento logs are located in the var/log
directory:
system.log
– General errors and warningsexception.log
– Catches exceptions
You can log custom messages using Magento’s built-in logger.
// Inject logger in your constructor public function __construct( \Psr\Log\LoggerInterface $logger ) { $this->logger = $logger; } // Log a custom message $this->logger->info('Something happened!'); $this->logger->error('Something went wrong!');
🔍 Enable Developer Mode
To show full error messages and stack traces, enable developer mode:
php bin/magento deploy:mode:set developer
This mode makes debugging easier — especially during development.
⚠️ Display Errors in Browser
To show PHP errors directly in the browser, edit index.php
:
error_reporting(E_ALL); ini_set('display_errors', 1);
🐞 Xdebug for Step Debugging
For advanced debugging, set up Xdebug with your IDE (e.g., PhpStorm or VS Code).
- Install Xdebug with PECL or manually
- Configure
php.ini
:zend_extension="xdebug.so" xdebug.mode=debug xdebug.start_with_request=yes xdebug.client_port=9003 xdebug.client_host=127.0.0.1
- Enable breakpoints in your IDE and run the script
🪵 Add Custom Log File
Want your own log file?
use \Magento\Framework\Filesystem\DriverInterface; $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log'); $logger = new \Zend_Log(); $logger->addWriter($writer); $logger->info('Logging to custom.log!');
🧹 Clear Logs
Clean up old logs to keep your system neat and fast:
php bin/magento log:clean
✅ Final Tips
- Use
var/report
for tracking specific errors by ID - Monitor
system.log
andexception.log
regularly - Use proper try/catch blocks to avoid white screens
Now you’re ready to debug like a pro. Whether it’s a missing semicolon or a stubborn plugin, Magento’s logging tools will help you track it down and fix it!