cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Following are my observer,

 

<?php
namespace Gta\CustomerMsgPdp\Observer\Product;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Data implements ObserverInterface
    {
        /**
         * Below is the method that will fire whenever the event runs!
         *
         * @param Observer $observer
         */
            public function execute(Observer $observer)
            {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerSession = $objectManager->get('Magento\Customer\Model\Session');
                if($customerSession->isLoggedIn()) 
                {

                    $product = $observer->getProduct();
                    $originalName = $product->getName();
                    $price = $product->getPrice();
                    if($price >= '50')
                    {
                        $modifiedName = $originalName . ' - Premium Products';
                    }
                    elseif($price <= '50')
                    {
                        $modifiedName = $originalName . ' - Non Premium Products';
                    }
                    // $modifiedName = $originalName . ' - Modified by Magento 2 Events and Observers';
                    $product->setName($modifiedName);
                }
            }
    }

I just want to add log in,

 

if($price >= '50')
                    {
                        $modifiedName = $originalName . ' - Premium Products';
............. log code .............. }

For Magento 1.9 added Mage::log($modifiedName, null, 'MyCustomLogfile.log',true) same how to do it magento 2.3?

5 REPLIES 5

Re: Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Hi @Aveeva,

There is not straight forward method for custom log file in magento2 like magento 1.x.

You need to follow the below flow.

create-a-custom-log-file-in-magento-2-using-monolog-and-logging-data-into-it/ 

If you want to use magento default log file then you can use following method.
devdocs/how-write-log-magento-2.html 


for more info:
https://devdocs.magento.com/guides/v2.3/config-guide/log/custom-logger-handler.html

I hope it will help you!

Re: Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Hello @Aveeva ,

Please refer the following link, https://www.google.com/amp/s/webkul.com/blog/create-a-custom-log-file-in-magento-2-using-monolog-and...

It will help you to create a custom log files and adding your log in it. If still you face any issue please let me know.

If this helps you, please accept it as solution and give kudos.

Regards.

Re: Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Re: Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

Hi @Aveeva 

 

It is just a hack but not recommended by magento to use this method. 

Re: Magento 2.3 - How to print log values inside the var/log/MyCustomLogfile.log?

@Aveeva You can do it in the following way.

 

Create di.xml inside etc folder of your module with the below code

 

<type name="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="name" xsi:type="string">test</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="test" xsi:type="object">NAME_SPACE\Test\Model\Logger\Handler\Debug</item>
            </argument>
        </arguments>
    </type>

Then create the handler inside Model folder  of your module.

<?php
/**
 * Copyright © 2017 Alshaya, LLC. All rights reserved.
 * See LICENSE.txt for license details.
 *
 */
namespace NAME_SPACE\Test\Model\Logger\Handler;

use Magento\Framework\Logger\Handler\Base;

/**
 * Log handler for reports
 */
class Debug extends Base
{
    /**
     * @var string
     */
    protected $fileName = '/var/log/test.log';
}

Then wherever you want to use log just Psr logger like this.

<?php
/**
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace NAME_SPACE\Test\Controller\Index;

use Psr\Log\LoggerInterface;
class Index extends \Magento\Framework\App\Action\Action
{


    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Show Contact Us page
     *
     * @return void
     */


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        LoggerInterface $logger
    ) {
        parent::__construct($context);
        $this->logger = $logger;
    }


    public function execute()
    {
        $this->logger->critical((string) 'Test');
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

It will log the all the data inside test.log.

 

Thanks