xiidea / easy-audit
一个用于记录选择事件的 Symfony 扩展包。它易于配置和定制以满足您的需求
Requires
- php: >=8.0.2
- psr/log: ^1|^2|^3
- symfony/framework-bundle: >=6.0 <7.0.0
- symfony/security-bundle: >=6.0 <7.0.0
Requires (Dev)
- doctrine/annotations: ^2.0
- doctrine/common: >=2.2 <4.0
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^9.5
- symfony/form: ^6.0
- symfony/test-pack: ^1.0
- symfony/twig-bundle: ^6.0
- symfony/validator: ^6.0
README
一个用于记录选择事件的 Symfony 扩展包。它易于配置和定制以满足您的需求。
版本
安装
- 在您的 composer.json 中添加 EasyAuditBundle
- 启用扩展包
- 创建 audit_log 实体类
- 配置 config.yml
- 更新数据库模式
1. 在您的 composer.json 中添加 EasyAuditBundle
在您的 composer.json 中添加 EasyAuditBundle
{ "require": { "xiidea/easy-audit": "^3.0" } }
现在运行以下命令让 composer 下载扩展包
$ php composer.phar update xiidea/easy-audit
Composer 将扩展包安装到您项目的 vendor/xiidea
目录。
2. 启用扩展包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Xiidea\EasyAuditBundle\XiideaEasyAuditBundle(), ); }
3. 创建 audit_log 实体类
XiideaEasyAuditBundle 默认支持 Doctrine ORM/MongoDB。但是,您必须提供一个具体的 AuditLog 类。请遵循 说明 来设置类
4. 配置 config.yml
您可以在 Resources/config/config-sample.yml
文件中找到示例配置数据
# app/config/config.yml xiidea_easy_audit: #resolver: xiidea.easy_audit.default_event_resolver #Optional #audit_log_class : MyProject\Bundle\MyBundle\Entity\AuditLog #Required #doctrine_event_resolver : xiidea.easy_audit.default_doctrine_event_resolver #Optional #default_logger : true #Optional #user property to use as actor of an event #valid value will be any valid property of your user class user_property : ~ # or username #Optional #List of doctrine entity:event you wish to track or set to false to disable logs for doctrine events # valid events are = [created, updated, deleted] #doctrine_objects : #Optional # MyProject\Bundle\MyBundle\Entity\MyEntity : [created, updated, deleted] # MyProject\Bundle\MyBundle\Entity\MyEntity2 : [] #List all events you want to track (Optional from v1.2.1 you can now use subscriber to define it) events : #Optional - security.interactive_login #List all custom resolver for event #custom_resolvers : # security.interactive_login : user.event_resolver #logger_channel: # xiidea.easy_audit.logger.service: ["info", "debug"] # file.logger: ["!info", "!debug"] #Custom Event Resolver Service services: #user.event_resolver: # class: Xiidea\EasyAuditBundle\Resolver\UserEventResolver # calls: # - [ setContainer,[ @service_container ] ]
5. 更新数据库模式
由于所有设置已完成,现在您需要更新数据库模式。为此,请从您的项目目录运行以下命令
$ php app/console doctrine:schema:update --force
核心概念
记录器
Logger
是负责持久化事件信息的核心服务。您可以根据需要定义尽可能多的记录器。EasyAudit 包含一个记录器服务 xiidea.easy_audit.logger.service
,这是默认记录器服务。您可以通过在配置中设置 default_logger: false
来轻松禁用该服务。
解析器
Resolver
类似于事件的翻译器。它用于将事件转换为 AuditLog 实体。EasyAudit 包含两个(2)解析器服务 xiidea.easy_audit.default_event_resolver
、xiidea.easy_audit.default_doctrine_event_resolver
。以及一个用于说明转换工作原理的定制 EventResolver 类 UserEventResolver
。您可以定义尽可能多的解析器服务并使用它们来处理不同的事件。在这里,您可以设置事件的严重级别。默认级别是 Psr\Log\LogLevel::INFO
。自定义严重级别不可用。EasyAudit 支持由 PSR-3 描述的日志级别。这些值用于基本过滤目的。您可以使用此值作为通道来注册不同的记录器以处理不同的事件。如果您向 AuditLog 对象添加任何其他字段,此位置可以添加这些额外信息(标签、元数据等)
通道
现在可以注册特定通道的记录器。通道指的是日志级别。您可以为 EasyAudit 记录器服务配置以仅处理特定级别的事件。
警告 - BC 破坏性更改
-
自 v1.2.2 以来,已删除
pre_persist_listener
选项。您可以使用 此食谱 来实现相同的功能 -
自 v1.2.2 以来,
EventResolverInterface
已分为EmbeddedEventResolverInterface
和EventResolverInterface
-
自 v1.3.x 以来,新的 Event 对象已适配。并且
EmbeddedEventResolverInterface
和EventResolverInterface
的签名也发生了变化。现在它期望额外的 $eventName 参数 -
自v1.4.7版本起,
EntityEventResolver
已重构为简化版本,如果您的代码直接依赖于旧版本实现,建议您从此处复制旧实现的内容 -
自v2.0版本起,FosUserBundle的事件从
UserEventResolver
以及使用Symfony\Contracts\*
命名空间的事件类中移除 -
自v3.0版本起,由于
Symfony\Component\Security\Core\Event\AuthenticationEvent
不再存在,因此也移除了security.authentication.failure
解析器
食谱
查看食谱以了解其他有趣的内容。