Magento2 自定义生成日志函数
/** * @param $info * @param string $file * @return bool */ public function logger($info, $file = "default.log") { if(!$this->log) return false; $baseUrl = str_replace('\\', DIRECTORY_SEPARATOR, realpath(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/')) . '/'; $path = explode('.', $file); $path = $path[0]; if(!is_dir($baseUrl.'/../var/log/'.$path)){ mkdir($baseUrl.'/../var/log/'.$path,0755); } $file = $path."-".date('Y-m-d',time()).'.'.pathinfo($file)['extension']; /** * @var $writer \Zend\Log\Writer\Stream * @var $logger \Zend\Log\Logger */ $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/'.$path.'/'.$file); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info(json_encode($info)); }