adrenalinkin / monolog-autowire-bundle
为 symfony/monolog-bundle 注册的日志记录器提供自动装配功能
v2.1.0
2019-04-28 21:31 UTC
Requires
- php: ~7.1
- psr/log: ^1.0
- symfony/config: ^3.4||^4.0
- symfony/dependency-injection: ^3.4||^4.0
- symfony/finder: ^3.4||^4.0
- symfony/http-kernel: ^3.4||^4.0
Suggests
- symfony/monolog-bundle: Install and enable bundle to activate main functionality of the current bundle
This package is auto-updated.
Last update: 2024-09-09 02:59:26 UTC
README
简介
该包提供通过标准 autowire
机制连接 MonologBundle
中注册的日志记录器的功能。这一目标通过自动生成的日志记录器类来实现。每个类装饰了注册的 monolog
通道中的一个对象。
此外,还有第二种实现目标的方法 - 使用 LoggerCollection
。如果请求的通道不存在,将选择回退 logger
。在这种情况下,回退 logger
将使用由 @Psr\Log\LoggerInterface
引用的服务。如果在这种情况下 logger
没有在服务容器中注册,将返回 Psr\Log\NullLogger
的实例。
重要: 在项目中没有 MonologBundle
的情况下,此包仍能正常工作。在这种情况下,LoggerCollection
将始终返回回退值。
安装
步骤 1:下载包
打开命令行控制台,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本
composer require adrenalinkin/monolog-autowire-bundle
此命令要求您全局安装 Composer。
步骤 2:启用包
然后,通过更新您的 app/AppKernel.php
文件来启用该包
<?php declare(strict_types=1); // app/AppKernel.php class AppKernel extends Kernel { // ... public function registerBundles() { $bundles = [ // ... new Linkin\Bundle\MonologAutowireBundle\LinkinMonologAutowireBundle(), ]; return $bundles; } // ... }
配置
要开始使用包,您不需要定义一些额外的配置。所有参数都有默认值
linkin_monolog_autowire: # directory where should be stored auto-generated loggers decorators loggers_dir: '%kernel.project_dir%/var/loggers' # path to loggers decorator template decorator_template: 'ChannelLogger.php.dist'
用法
假设我们的项目有以下配置 MonologBundle
monolog: handlers: doctrine: type: stream path: "%kernel.logs_dir%/%kernel.environment%.doctrine.log" level: info channels: - "doctrine" acme: type: stream path: "%kernel.logs_dir%/%kernel.environment%.acme_channel.log" level: info channels: - "acme_channel"
通过自动生成的日志记录器使用
类名基于通道名称生成。所有非字母数字值都将被删除,并将名称转换为 CamelCase
格式。所有类都以 Channel
开头并以 Logger
结尾。
<?php declare(strict_types=1); use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelAcmeLogLogger; use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelDoctrineLogger; use Psr\Log\LoggerInterface; class AcmeLoggerAware { /** * @var ChannelDoctrineLogger */ private $acmeLogLogger; /** * @var ChannelDoctrineLogger */ private $doctrineLogger; /** * @var LoggerInterface */ private $logger; /** * @param ChannelAcmeLogLogger $acmeLogLogger * @param ChannelDoctrineLogger $doctrineLogger * @param LoggerInterface $logger */ public function __construct( ChannelAcmeLogLogger $acmeLogLogger, ChannelDoctrineLogger $doctrineLogger, LoggerInterface $logger ) { $this->acmeLogLogger = $acmeLogLogger; $this->doctrineLogger = $doctrineLogger; $this->logger = $logger; } public function doSome(): void { $this->acmeLogLogger->info('INFO into "acme_log" channel'); $this->doctrineLogger->info('INFO into "doctrine" channel'); $this->logger->info('INFO into Fallback or into NullLogger'); } }
通过日志记录器集合使用
<?php declare(strict_types=1); use Linkin\Bundle\MonologAutowireBundle\Collection\LoggerCollection; use Psr\Log\LoggerInterface; class AcmeLoggerAware { /** * @var LoggerInterface */ private $acmeLogLogger; /** * @var LoggerInterface */ private $doctrineLogger; /** * @var LoggerInterface */ private $logger; /** * @param LoggerCollection $loggerCollection */ public function __construct(LoggerCollection $loggerCollection) { $this->acmeLogLogger = $loggerCollection->getLogger('acme_log'); $this->doctrineLogger = $loggerCollection->getLogger('doctrine'); $this->logger = $loggerCollection->getLogger(); } public function doSome(): void { $this->acmeLogLogger->info('INFO into "acme_log" channel'); $this->doctrineLogger->info('INFO into "doctrine" channel'); $this->logger->info('INFO into Fallback or into NullLogger'); } }