Skip to content

[Magento 2] Dirty Way to Create Own Log File

Yes, it’s dirty…not a Magento 2 standard… so it just for who love ‘shortcut’ way to create own log file, let’s get started

namespace  Company\ModuleName\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
    protected $directory_list;
    public function __construct(
       \Magento\Framework\App\Filesystem\DirectoryList $directory_list
    ) {
         $this->directory_list = $directory_list;

    public function setLog($string,$logFile = 'your-logfile-name.log')

        $var_DIR = $this->directory_list->getPath('var');
        file_put_contents($var_DIR.'/log/'.$logFile, "\n"."[".$this->_magentoDate->gmtDate()."] ".$string, FILE_APPEND);



Comments are closed.