whitedigital-eu/audit-service
Composer 安装命令:
composer require whitedigital-eu/audit-service
包简介
Audit Service
README 文档
README
IMPORTANT: When migrating from
0.4(or earlier) to0.5, configuration and usage has changed slightly:
- When used outside of this package,
AuditServicecan now be accessed asWhiteDigital\Audit\Contracts\AuditServiceInterfaceinstead ofWhiteDigital\Audit\Service\AuditServiceLocator.AuditServiceLocatorclass has been removed.- Configuration has changed and now it is one level higher:
Old version:whitedigital: audit: enable: true audit_entity_manager: audit default_entity_manager: defaultuse Symfony\Config\WhitedigitalConfig; return static function (WhitedigitalConfig $config): void { $config ->audit() ->enabled(true) ->auditEntityManager('audit') ->defaultEntityManager('default'); };New version:
audit: audit_entity_manager: audit default_entity_manager: defaultIf used in php config, it is now
AuditConfiginstead ofWhitedigitalConfiguse Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->auditEntityManager('audit') ->defaultEntityManager('default'); };As this bundle now comes enabled,
enabledparameter has been removed
What is it?
Audit is service needed to audit (log) events into database. For now package only ships with doctrine implementation to save data, but you can easily extend functionality (see below) to use other means of storage.
System Requirements
PHP 8.1+
Symfony 6.3+
Project Requirements
2 separate Doctrine entity managers (if using provided AuditService)
Installation
The recommended way to install is via Composer:
composer require whitedigital-eu/audit-service
Configuration
Configuration differs between default setup and overridden one. If you are interested in configuration for overriding part of package, scroll down to appropriate section.
audit: audit_entity_manager: audit default_entity_manager: default
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->auditEntityManager('audit') ->defaultEntityManager('default'); };
audit_entity_manageris entity manager used for audit
default_entity_manageris entity manager you use for database operations
Logic is split between 2 managers because it is easier to audit database exception this way. If done with one entity manager, a lot extra steps need to be taken (opening closed entity manager, checking status of operations, etc.)
After this, you need to update your database schema to use Audit entity.
If using migrations:
bin/console doctrine:migrations:diff bin/console doctrine:migrations:migrate
If by schema update:
bin/console doctrine:schema:update --force
This is it, now you can use audit. It is configured and autowired as AuditServiceInterface.
use WhiteDigital\Audit\Contracts\AuditType; use WhiteDigital\Audit\Contracts\AuditServiceInterface; public function __construct(private AuditServiceInterface $audit){} $this->audit->audit(AuditType::EXCEPTION, 'something happened'); try { somefunction(); } catch (Exception $exception){ $this->audit->auditException($exception); }
Audit service comes with 2 event subscribers: one for exceptions and one for database events.
Exception subscriber:
By default exception subscriber audits all exceptions, except 404 response code. you can override this logic by:
audit: excluded: response_codes: - 404 - 405
use Symfony\Config\AuditConfig; use Symfony\Component\HttpFoundation\Response; return static function (AuditConfig $config): void { $config ->excluded() ->responseCodes([ Response::HTTP_NOT_FOUND, Response::HTTP_METHOD_NOT_ALLOWED, ]); };
By default exception subscriber audits all exceptions on all routes and paths. you can override this logic by: Path:
audit: excluded: paths: - '/test'
use Symfony\Config\AuditConfig; use Symfony\Component\HttpFoundation\Response; return static function (AuditConfig $config): void { $config ->excluded() ->paths([ '/test', ]); };
Route:
audit: excluded: routes: - 'app_test'
use Symfony\Config\AuditConfig; use Symfony\Component\HttpFoundation\Response; return static function (AuditConfig $config): void { $config ->excluded() ->routes([ 'app_test', ]); };
Allowed Audit types
To not to create chaos within audit records, it is only allowed to use specified audit types.
Default values are: AUTHENTICATION, DATABASE, ETL_PIPELINE, EXCEPTION and EXTERNAL_CALL.
If you don't have any custom types, you can use
WhiteDigital\Audit\Contracts\AuditTypeclass for default constants.
If you wish to add more types, configure new types as so:
audit: additional_audit_types: - test1 - test2
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->additionalAuditTypes([ 'test1', 'test2', ]); };
It is possible to run symfony command that generates interface based on default and added types for easier code complete:
bin/console make:audit-types
This command will generate new file based on package configuration. By default, this command will make
App\Audit\AuditType class. You can override this name in configuration:
audit: audit_type_interface_namespace: 'App\Audit' audit_type_interface_class_name: 'AuditType'
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->auditTypeInterfaceNamespace('App\Audit') ->auditTypeInterfaceClassName('AuditType'); };
If you have this defined interface and want to add more allowed types, you can just add new types to it without adding types to package configuration. Allowed types will be merged from configuration and interface.
Api Resource
This package comes with AuditResource for api-platform, it's iri is /api/audits.
Overriding parts of bundle
Overriding audit service
If you wish not to use audit service this package comes with, you can override it with your own.
To do so, implement AuditServiceInterface into your service:
use WhiteDigital\Audit\Contracts\AuditServiceInterface; class YourAuditService implements AuditServiceInterface { public function audit(string $type, string $message, array $data = [], string $class = ''): void { } public function auditException(Throwable $exception, ?string $url = null, string $class = ''): void { } public static function getDefaultPriority(): int { return 2; } }
AuditService in this package comes with priority of 1. To override it, make sure to add priority higher than that.
If your custom AuditService does not use database as an audit storage, you need to disable part of this package that requires 2 entity managers. You can do it like this:
audit: custom_configuration: true
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->customConfiguration(true); };
Overriding default api resource (and therefore api endpoints)
By default, Audit resource is based on AuditResource
If you wish not to use this resource and not expose the api endpoints it provides, just set a custom api resource path
with a configuration value. If you set it as null, api platform will not register api resource located within this
package.
audit: custom_api_resource_path: '%kernel.project_dir%/src/MyCustomPath' # custom_api_resource_path: null
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->customApiResourcePath('%kernel.project_dir%/src/MyCustomPath') // or ->customApiResourcePath(null); };
After overriding default api resource, do not forget to update ClassMapperConfigurator configuration that is used for
resource <-> entity mapping in whitedigital-eu/entity-resource-mapper-bundle
use App\ApiResource\Admin\AuditResource; use WhiteDigital\Audit\Entity\Audit; use WhiteDigital\EntityResourceMapper\Mapper\ClassMapper; use WhiteDigital\EntityResourceMapper\Mapper\ClassMapperConfiguratorInterface; final class ClassMapperConfigurator implements ClassMapperConfiguratorInterface { public function __invoke(ClassMapper $classMapper): void { $classMapper->registerMapping(AuditResource::class, Audit::class); } }
Overriding default entity
By default, Audit entity is based on BaseEntity that comes from whitedigital-eu/entity-resource-mapper-bundle.
If you wish not to use this base at all, you need to create new Entity and implement AuditEntityInterface into it:
use Doctrine\ORM\Mapping as ORM; use WhiteDigital\Audit\Contracts\AuditEntityInterface; #[ORM\Entity] class AuditEntity implements AuditEntityInterface {}
or you can use Model from package:
use Doctrine\ORM\Mapping as ORM; use WhiteDigital\Audit\Model\Audit; #[ORM\Entity] class AuditEntity extends Audit {}
If you wish to add new properties or, maybe, use different name for Audit entity that comes from package:
use Doctrine\ORM\Mapping as ORM; use WhiteDigital\Audit\Entity\Audit as BaseAudit; #[ORM\Entity] #[ORM\Table('new_audit_table_name')] class AuditEntity extends BaseAudit {}
Now when you use audit() or auditException() functions in your project, you need to tell service to
use your entity:
use WhiteDigital\Audit\Contracts\AuditType; use WhiteDigital\Audit\Contracts\AuditServiceInterface; use App\Entity\AuditEntity; // example public function __construct(private AuditServiceInterface $audit){} $this->audit->audit(AuditType::EXTERNAL, 'something happened', [], AuditEntity::class); $this->audit->audit(AuditType::ETL, 'something happened', class: AuditEntity::class); try { somefunction(); } catch (Exception $exception){ $this->audit->auditException($exception, '', AuditEntity::class); $this->audit->auditException($exception, class: AuditEntity::class); }
This bundle automatically adds WhiteDigital\Audit\Entity namespace to both given entity managers.
If you wish to not it to do it, for example, if you don't use default entity or maybe don't store
audits in database at all, you need to configure this to disable it:
audit: set_doctrine_mappings: false
use Symfony\Config\AuditConfig; return static function (AuditConfig $config): void { $config ->setDoctrineMappings(false); };
Overriding auditing of entity events
You can disable entity event auditing in runtime by calling setIsEnabled setter for entity event subscriber
use WhiteDigital\Audit\EventSubscriber\AuditDoctrineEventSubscriber; public function __construct(private AuditDoctrineEventSubscriber $subscriber){} $this->subscriber->setIsEnabled(false); someFunction(); $this->subscriber->setIsEnabled(true);
PHP CS Fixer
IMPORTANT: When running php-cs-fixer, make sure not to format files in
skeletonfolder. Otherwise maker command will stop working.
whitedigital-eu/audit-service 适用场景与选型建议
whitedigital-eu/audit-service 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 889 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「Audit」 「api-platform」 「symfony-bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 whitedigital-eu/audit-service 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 whitedigital-eu/audit-service 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 whitedigital-eu/audit-service 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
The bundle for easy using json-rpc api on your project
PB Web Media Audit Bundle for Symfony
Bundle Symfony DaplosBundle
Simplest Auditable for Laravel Eloquent/Model
统计信息
- 总下载量: 889
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-12