Zend Framework 3 根据日志级别写日志

Writing logs based on log level in Zend Framework 3

我们最近开始将一个项目从 Zend Framework 1 迁移到 Zend Framework 3。对于 Zend Framework 1,我们使用 Log4PHP 库进行应用程序日志记录。使用 Log4PHP,我们能够根据日志级别(调试、信息和错误)将日志记录在不​​同的文件中。

Log4PHP 库与 Zend Framework 3 不兼容。因此决定使用 Zend Framework 3 中可用的 Zend Log 组件。我试图通过查看 Internet 上可用的文档来根据日志级别放置日志,并注意到所有日志都被写入指定为写入器一部分的所有提到的文件。

HomeControllerFactory.php

    <?php
    namespace Application\Controller;

    use Zend\ServiceManager\Factory\FactoryInterface;
    use Interop\Container\ContainerInterface;
    use Zend\Log\Logger;
    use Zend\Log\Writer;

    class HomeControllerFactory implements FactoryInterface
    {
        public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
        {
            $serviceManager = $container->get('ServiceManager');
            $logger = $container->get(Logger::class);
            $infoWriter = new Writer\Stream('C:\log\info.log');
            $debugWriter = new Writer\Stream('C:\log\debug.log');
            $errorWriter = new Writer\Stream('C:\log\error.log');
            $logger->addWriter($infoWriter,6);
            $logger->addWriter($debugWriter,7);
            $logger->addWriter($errorWriter,3);
            return new HomeController($serviceManager, $logger);
        }
    }

HomeController.php

    <?php
    /**
     * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source     repository
     * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
     * @license   http://framework.zend.com/license/new-bsd New BSD License
     */
    
    namespace Application\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    use \Zend\ServiceManager\ServiceManager;
    use Zend\Log\Logger;
    use Application\Model\UTP\UTPUtils;
    
    class HomeController extends AbstractActionController {
    
        private $serviceManager;
        private $logger;
    
        public function __construct(ServiceManager $serviceManager, Logger $objLogger)
        {
            $this->serviceManager = $serviceManager;
            $this->logger = $objLogger;
        }
    
        public function indexAction() {
            $config = $this->serviceManager->get('config');

            $this->logger->info(__CLASS__ . ' It is an info log');
            $this->logger->debug(__CLASS__ . ' It is a debug log');
            $this->logger->err(__CLASS__ . ' It is an error log');
        }

    }

有了这个,所有三个日志都写入了所有 3 个文件 error.log、info.log、debug.log 我想要实现的是,如果我们调用

也许您可以 Monolog 库而不是 Zend Log? 然后你可以写:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('C:\log\info.log', Logger::INFO));
$log->pushHandler(new StreamHandler('C:\log\debug.log', Logger::DEBUG));
$log->pushHandler(new StreamHandler('C:\log\error.log', Logger::ERROR));

// add records to the log
$log->warning('Foo');
$log->error('Bar');

并且您的代码将与 PSR-3 标准兼容