maciejkosiarski / monolog-factory-bundle

注入式Monolog工厂的Bundle,允许在注入后选择特定的记录器

dev-master 2019-02-11 11:55 UTC

This package is auto-updated.

Last update: 2024-09-13 21:31:44 UTC


README

使用Symfony Flex的应用程序

打开命令行,进入你的项目目录并执行

$ composer require maciejkosiarski/monolog-factory-bundle

未使用Symfony Flex的应用程序

步骤 1: 下载Bundle

打开命令行,进入你的项目目录并执行以下命令以下载此Bundle的最新稳定版本

$ composer require maciejkosiarski/monolog-factory-bundle

此命令需要你在全局范围内安装Composer,如Composer文档中的安装章节所述。

步骤 2: 启用Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的Bundle列表来启用该Bundle

<?php
// config/bundles.php

return [
    // ...
    MaciejKosiarski\MonologFactoryBundle\MonologFactoryBundle::class => ['all' => true],
    // ...
];

作为服务注册在 config/services.yaml 中,并配置为自动注入

// ...

services:

// ...

    MaciejKosiarski\MonologFactoryBundle\Service\LoggerFactory:
        autowire: true

在控制器中

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstarctController;

class AppController extends AbstarctController
{
    public function index(LoggerFactory $logger)
    {
        $this->logger = $logger->getLogger('your_channel');
    }
    
    // ...
    
}

在命令中

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use MaciejKosiarski\MonologFactoryBundle\Service\LoggerFactory;

class AppCommand extends Command
{
    private $Logger;
	
    public function __construct(LoggerFactory $logger)
    {
        parent::__construct();

        $this->logger = $logger->getLogger('your_channel');
    }
    
    protected function configure()
    {
    	// ...
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
    	$this->logger->info('Process started', []);
    	// ...
    }
    
    // ...

}