How to write logs in var/log/whatever_file from a phtml file which belongs to my custom theme?
3 Answers
If you want to create a new file for log, use the following code.
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
If you want to add a log in the existing log file of Magento, use the following code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logger = $objectManager->get("Psr\Log\LoggerInterface");
$logger->debug('debug log example'); // add logs in debug.log
$logger->info('system log example'); // add logs in system.log
- 6,447
- 2
- 23
- 50
-
Thanks! How to you put it in the debug.log file instead ? – DevonDahon Nov 06 '17 at 07:52
-
I have updated my answer. Please check. – Dinesh Yadav Nov 06 '17 at 08:04
-
1objectmanager? Really? https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly – CompactCode Nov 06 '17 at 08:05
-
@SwAt.Be What's the recommended way ? – DevonDahon Nov 06 '17 at 08:10
-
1I have posted it in an answer beneath , it took me longer then 2 seconds to reply but someone here is out for points @DineshYadav instead of giving a good answer. – CompactCode Nov 06 '17 at 08:27
Wrong way :
- Using classes and objectmanager directly in your phtml
Good way :
Find the .xml that outputs your .phtml file. This can be find in the same Magento Module within the folder 'view/frontend/layout' where you find the .phtml file. You will find a Block somewhere with a class and a template much like this :
(i will use this as an example)
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="Magento_Catalog::product/view/attribute.phtml" after="product.info.type">
This has a class and a template. What you want to do is add functionality to that class which logs it for you.
Now let's create a new PHP file that we will use instead of this one.
create a basic module How to create a basic module
add a
di.xmlwithin your etc/ folder in your new module
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Product\View\Description" type="YourVendor\YourModule\Block\Product\View\Description"/> </config>
(is in a 'qoute' instead of a 'code' because stackexchange won't show the code for some reason otherwise)
Here we define that we want to replace this class with our own whenever this is used. If we just want to replace it for that 1 instance you can adjust the class in your layout xml that is reponsible for creating the block and adjust the class there. Common layout tasks
Now we need to create our PHP class :
app/code/yourvendor/yourmodule/Block/Product/View/Description.php
<?php
namespace yourvendor/yourmodule/Block/Product/View;
use Magento\Catalog\Block\Product\View\Description as MagentoDescription;
use yourvendor/yourmodule/Logger/YourLogger;
class Description extends MagentoDescription{
protected $_logger;
public function __construct (
... ,
YourLogger $logger){
$this->_logger = $logger;
}
// add new function to log things (or do anything)
public function addLogCritical(\Exception $e){
$this->_logger->critical('an error has occured :' . $e);
}
}
Here we added our own logger function and used a new logger class.
Now we will make our logger class (that we can use anywhere from now on) to create or own custom file.
YourVendor/YourModule/Logger/YourLogger.php
<?php
namespace YourVendor\YourModule\Logger;
use Monolog\Logger as MonologLogger;
class YourLogger extends MonologLogger
{
}
We leave this empty but we use the Monolog Logger. We can add function in here later if needed.
Now create a Handler
YourVendor/YourModule/Logger/Handler.php
<?
namespace YourVendor\YourModule\Logger;
use Magento\Framework\Logger\Handler\Base;
class Handler extends Base {
/**
* Logging level
* @var int
*/
protected $loggerType = Logger::DEBUG;
/**
* File name
* @var string
*/
protected $fileName = '/var/log/customfolder/customname.log';
}
And now we bind them in our YourVendor/YourModule/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="YourVendor\YourModule\Logger\Handler">
<arguments>
<argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
</arguments>
</type>
<type name="YourVendor\YourModule\Logger\YourLogger">
<arguments>
<argument name="name" xsi:type="string">DesignitSync</argument>
<argument name="handlers" xsi:type="array">
<item name="system" xsi:type="object">YourVendor\YourModule\Logger\Handler</item>
</argument>
</arguments>
</type>
</config>
And done! Now you have your own custom logger the good way.
You can use your new public function in your phtml file with $block->addLogCritical('error message');
- 2,467
- 1
- 20
- 36
-
I have added the frontend/di.xml code , it wasn't showing for some odd reason. Had to put it in a quote instead of a code in the stackexchange editor – CompactCode Nov 06 '17 at 08:42
-
1I hope you learned a lot about what the difference is between bad practices and good practices in Magento 2 – CompactCode Nov 07 '17 at 03:57
Try the below code to write custom code in .phtml file
for Magento 2.4.2 before version use this
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/customlog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Custom message');
for Magento 2.4.2 or after version use this
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/customlog.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('text message');
for Magento 2.4.3 version use this
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customlog.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
- 340
- 1
- 13