0

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.

3
  • Please refer to magento.stackexchange.com/questions/75935/… Jun 9 '20 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 '20 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 '20 at 8:03
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));
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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