1

I wanted to add custom log files in separate folders. I have created one helper to create log and calling every time that helper.

Here is the helper file :

<?php

namespace Custom\Product\Helper;
use Magento\Framework\Filesystem;

class CustomLog {

    protected $_fileSystem;

    public function __construct(
        Filesystem $file_system
    ){
        $this->_fileSystem  = $file_system;
    }

    public function Clog($folder_name,$file_name,$data)
    {
        $directory = BP.'/var/log/'.$folder_name.'/'.$file_name;
        $writer = new \Zend\Log\Writer\Stream(BP . $directory);
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($data);
    }


}

and I have tried with filesystem also still folder , files are not creating.

Here is the file system code :

public function Clog($folder_name,$file_name,$data)
    {
        $directory = BP.'/var/log/'.$folder_name;
        $writer = $this->_fileSystem->getDirectoryWrite($directory);
        $file = $writer->openFile($file_name, 'w');

    }

Please help me to resolve this issue .

0

Follow below steps to create your own custom log file under 'project_dir'/var/log. Step 1: Write Logger class in Logger/Logger.php

<?php
namespace YourNamespace\YourModule\Logger;

class Logger extends \Monolog\Logger
{
}

Step 2: Write Handler class in Logger/Handler.php

<?php
namespace YourNamespace\YourModule\Logger;

use Monolog\Logger;

    class Handler extends \Magento\Framework\Logger\Handler\Base
    {
        /**
         * Logging level
         * @var int
         */
        protected $loggerType = Logger::INFO;

        /**
         * File name
         * @var string
         */
        protected $fileName = '/var/log/myfilename.log';
    }

Step 3 : Register Logger in Dependency Injection etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="YourNamespace\YourModule\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="YourNamespace\YourModule\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">myLoggerName</argument>
            <argument name="handlers"  xsi:type="array">
                <item name="system" xsi:type="object">YourNamespace\YourModule\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>

Step 4 : Use the logger in your Magento classes - This is done by Dependency Injection. Below you will find a dummy class which only writes a log entry

<?php
namespace YourNamespace\YourModule\Model;

class MyModel
{
    /**
     * Logging instance
     * @var \YourNamespace\YourModule\Logger\Logger
     */
    protected $_logger;

    /**
     * Constructor
     * @param \YourNamespace\YourModule\Logger\Logger $logger
     */
    public function __construct(
        \YourNamespace\YourModule\Logger\Logger $logger
    ) {
        $this->_logger = $logger;
    }

    public function doSomething()
    {
        $this->_logger->info('I did something');
    }
}

Hope its Helps!

| improve this answer | |
  • thanks Shireen , For this we can create one file only but for me every call need to create new file – user53836 Feb 28 '18 at 12:08
0

Try this :

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/folder_name/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your Log'); // Simple Text Log
| improve this answer | |
0

Finally i got the solution ,

Here is the code :

<?php

namespace Custom\Product\Helper;
use Magento\Framework\Filesystem\Io\File;

class CustomLog {

    protected $_io;

    public function __construct(
        File $io
    ){
        $this->_io = $io;
    }

    public function Clog($folder_name,$file_name,$data)
    {
        $directory = BP.'/var/log/'.$folder_name;
        $this->_io->mkdir($directory, 0777);
        $log_file   = "/var/log/".$folder_name."/".$file_name;
        $writer = new \Zend\Log\Writer\Stream(BP . $log_file);
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($data); 
    }


}

This creating new folder , file and appending the data also.

| improve this answer | |

Your Answer

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