small / logger-bundle
Symfony 日志记录器
22.1.0
2024-09-09 02:58 UTC
Requires
- php: >=8.0
- small/logger: 22.*
README
small-logger-bundle
Symfony small logger
实际上,它只是 symfony 和 small-logger 之间的桥梁。
此外,此包实现了 symfony 原生 HTTP 客户端用于 HTTP 输出。
安装
使用 composer 在您的 symfony 项目中安装此包
$ composer require small/logger-bundle
配置
首先为 SwitchLogicInterface 定义服务
# config/service.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
Small\Logger\Contracts\SwitchLogicInterface:
class: Small\Logger\SwitchLogic\DefaultSwitchLogic
small-logger 包中有两个默认开关可用
- Small\Logger\SwitchLogic\DefaultSwitchLogic -> 将 BasicLog 记录到标准输出
- Small\Logger\SwitchLogic\CommonLogSwitchLoggic -> 将 CommonLog 记录到文件
参阅 small-logger 文档 创建自己的开关、流、格式化器或日志
创建快捷方式
快捷方式是简化日志写入的回调函数。
您需要在类的构造函数中声明它们。例如
namespace App\Log;
use Small\Logger\Contracts\LogInterface;
use Small\Logger\Log\BasicLog;
use Small\LoggerBundle\Service\Logger;
class Shortcuts
{
public function __construct(Logger $logger)
{
$logger->registerShortcut('info', function(Logger $logger, $message) {
$logger->log(new BasicLog(new \DateTime(), LogInterface::ERR_LEVEL_INFO, $message));
});
}
}
并在配置中声明 Shortcuts 类
# config/packages/small_logger.yaml
small_logger:
shortcuts:
- App\Log\Shortcuts
声明回调后,只需通过记录器调用即可
<?php
namespace App\Controller;
use Small\LoggerBundle\Service\Logger;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class TestLog extends AbstractController
{
/**
* @Route("/log")
* @param Dao $daoFactory
* @param Logger $logger
* @return Response
*/
public function logAction(Logger $logger)
{
$logger->info('This is a message');
return new Response("That's done !");
}
}