1

Can anyone help me in creating custom logger file in mmagento 2. I want to display collection object in log file. So far, i have tried this code but did not work. getting $collection object from public function massAction(AbstractCollection $collection)

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/pdf.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info("Hello");
    $logger->info(print_r($collection->debug())); 

This is what i have tried yet.

4
  • Please refer to magento.stackexchange.com/questions/75935/… Jun 9, 2020 at 7:54
  • @SanjibChowdhury i am getting this error in exception.log file SQLSTATE[HY000] [2002] No such file or directory {"exception":"[object] (Zend_Db_Adapter_Exception(code: 2002): SQLSTATE[HY000] [2002] No such file or directory at /srv/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php:144, PDOException(code: 2002): SQLSTATE[HY000] [2002] No such file or directory at /srv/public_html/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php:128)"} []
    – Nafsss
    Jun 9, 2020 at 7:56
  • SQLSTATE[HY000] [2002] comes due to DB connection failed. please check if your database engine is working fine or not. Jun 9, 2020 at 8:03

3 Answers 3

1

Use the following code for Magento versions prior to 2.4.2.

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Custom message') 
$logger->info(print_r($object->getData(), true));

Use the following code for Magento 2.4.2

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new  \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('text message');

Use the following code for Magento 2.4.3

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('text message');
0

Create Module/Namespace/etc/di.xml and add below config in the file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">


  <!-- Log generation process -->
<type name="Module\Namespace\Logger\Handler">
    <arguments>
        <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
    </arguments>
</type>
<type name="Module\Namespace\Logger\Logger">
    <arguments>
        <argument name="name"      xsi:type="string">Module/Namespace</argument>
        <argument name="handlers"  xsi:type="array">
            <item name="debug"     xsi:type="object">Module\Namespace\Logger\Handler</item>
        </argument>
    </arguments>   
</type>

Create Module/Namespace/Logger/Handler.php

    namespace Module\Namespace\Logger;

    use Magento\Framework\Filesystem\DriverInterface;
    use Magento\Framework\Logger\Handler\Base;
    use Magento\Framework\Stdlib\DateTime\TimezoneInterface;  
    class Handler extends Base {

        public function __construct(
            DriverInterface $filesystem
        ) {

            $this->fileName = "/var/log/pdf.log";
            parent::__construct($filesystem);
        }

    }

Create Module/Namespace/Logger/Logger.php

class Logger extends \Monolog\Logger
{

}

Now use Module\Namespace\Logger\Logger instead of Psr\Log\LoggerInterface;

namespace Module\Namespace\Model;

use Module\Namespace\Logger\Logger;


class Association
{


     /**
     * @var Logger
     */
    protected $logger;

    /**
     * __construct
     * 
     * @param Logger $logger
     */
    public function __construct(

        Logger $logger
    )
    {


        $this->logger = $logger;
    }


    /**
     * Generating log
     * 
     * @param string $email
     * @param string $name
     */
    public function generateLog($email, $name)
    {
        $logMessage = array(
            'Email' => $email,
            'Name' => $name
        );

        $this->logger->log(100, json_encode($logMessage));
    }
0

You can use any of the following methods. sure one of them will work.

**Method 1:**  

    $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/customfile.log');
    $logger = new \Zend_Log();
    $logger->addWriter($writer);
    $logger->info('I am logged from Zend_log_writer');
   
**Method 2:**

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logfileZend.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info('Simple Text Log from Zend logger 2'); // Simple Text Log

**Method 3:**

    $writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/customlaminas.log');
    $logger = new \Laminas\Log\Logger();
    $logger->addWriter($writer);
    $logger->info("log from laminas log");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.