minvws/audit-logger
Composer 安装命令:
composer require minvws/audit-logger
包简介
Generic audit logger
README 文档
README
This package provides a generic logging service for the RDO platform. It allows to easily log events to the database, syslog or other destinations.
Log events can be logged with or without PII data. PII data is data that can be used to identify a person. Depending on the logging destination, it can send the PII data or not.
Data can be automatically encrypted with a public/private keypair so that logging can be written, but not directly read.
This package is a generic module that isn't directly coupled to a framework, however, there are other packages that provide a framework specific implementation.
Installation
Requirements
- PHP >= 8.3
- Composer
Composer
You can install the package via the composer package manager:
composer require minvws/audit-logger
Configuration
PSR logging
With the PSRLogger you can log through any PSR compatible logger. This makes it easy to log to any destination like syslog, or monolog.
Encryption
Audit lines can optionally be encrypted so they can only be read by the application that has the private key.
To generate a new keypair, use the following code:
$kp = sodium_crypto_box_keypair(); $pubkey = sodium_crypto_box_publickey($kp); $privkey = sodium_crypto_box_secretkey($kp); echo "Public key: " . base64_encode($pubkey) . "\n"; echo "Private key: " . base64_encode($privkey) . "\n";
Logging full request
The option log_full_request can be used to log the full HTTP request. Any sensitive information
like passwords, tokens etc should be stripped from the request. However, there is no guarantee that
all sensitive information will be stripped if the naming is different.
However, this configuration option is NOT implemented. You MUST call logFullRequest() on the event
in order to log the full request.
Usage
To use the logger, inject or resolve the LogService class. This class has a single method:
$logger = app(LogService::class); $logger->log((new UserLoginLogEvent()) ->asExecute() ->withActor($user) ->withData(['foo' => 'bar']) ->withPiiData(['bar' => 'baz']) ->withFailed(true, 'invalid login') );
To create an event, you can use the fluent interface to create the event. Two elements are always required: the action code and the actor.
The action code is a string that describes the action that was performed. This could be 'create', 'delete', 'update', 'read' or 'execute'.
These are defined with the ->asCreate(), ->asDelete(), ->asUpdate(), ->asRead() and ->asExecute() methods.
Next, we need to define the actor that performed the action. This can be a user, a client or a system. This is done with the ->withActor() method.
It will need a user that implements the LoggableUser interface.
The event can have some additional fields:
->withTarget can be used to define the target of the action. This can be a user, a client or a system. This can be empty if the actor and target
are the same, but different when an actor performs an action onto a target (for instance, an admin (actor) creates a regular user (target)).
->withData can be used to add additional data to the event. This can be any data that is relevant to the event.
->withPiiData can be used to add PII data to the event. This can be any data that is relevant to the event and contains PII data. Depending on
the logging configuration, this data might or might not be send to a logger destination.
->withFailed can be used to mark the event as failed. This will add the failed field to the event and set it to true. The second parameter
can be used to add a reason for the failure.
->withSource can be used to add a source to the event. This can be any string that describes the source of the event. It's often nothing more
than the name of the application (->withSource(config('app.name'))).
->withEventCode can be used to add an event code to the event. This can be any string that describes the event. Often it's already defined by the
event class, but it can be overridden.
->logFullRequest can be used to log the full HTTP request. Any sensitive information like passsords, tokens etc will be stripped from the request.
Events
Each event has a unique event code. This code is used to identify the event and can be used to filter events in your logs. For rabbitMQ logging, each event will have a unique routing key that can be used to route events to different queues.
| Class | Event code | routing key | Description |
|---|---|---|---|
| DeclarationLogEvent | 080001 | declaration | Declaration event |
| LogAccessEvent | 080002 | log_access | Accessing logs |
| VerificationCodeDisabledLogEvent | 080003 | verification_code_disabled | Disabled verification code |
| RegistrationLogEvent | 080004 | registration | Registration event |
| ------ | -------- | ------------------------------ | --------------------------------- |
| AccountChangeLogEvent | 090001 | account_change | generic user account changes |
| UserCreatedLogEvent | 090002 | user_created | created new user |
| ResetCredentialsLogEvent | 090003 | reset_credentials | reset user credentials |
| ActivateAccountLogEvent | 090004 | activate_account | Activate account |
| AdminPasswordResetLogEvent | 090005 | admin_password_reset | Admin password reset |
| AmpUploadEvent | 090006 | amp_upload | AMP upload |
| ---- | ---------- | ------------------------------ | --------------------------------- |
| OrganisationCreatedLogEvent | 090012 | organisation_created | Created new organisation |
| OrganisationChangedLogEvent | 090013 | organisation_changed | Updated organisation (name) |
| -- | ------------ | ------------------------------ | --------------------------------- |
| AccountChangeLogEvent1 | 900101 | account_change | changed user data |
| AccountChangeLogEvent | 900102 | account_change | changed roles |
| AccountChangeLogEvent | 900103 | account_change | changed timeslot |
| AccountChangeLogEvent | 900104 | account_change | changed active enabled/disabled |
| AccountChangeLogEvent | 900105 | account_change | reset credentials |
| -------------- | ------------------------------ | --------------------------------- | --------------------------------- |
| AccountChangeLogEvent | 900201 | account_change | changed kvtb user data |
| AccountChangeLogEvent | 900202 | account_change | changed kvtb roles |
| AccountChangeLogEvent | 900203 | account_change | reset kvtb credentials |
| -------------- | ------------------------------ | --------------------------------- | --------------------------------- |
| UserLoginLogEvent | 091111 | user_login | user login |
| UserLogoutLogEvent | 092222 | user_logout | user logout |
| UserLoginTwoFactorFailedEvent | 093333 | user_login_two_factor_failed | user login 2fa failed |
Creating custom events
Creating a custom event is easy. You can create a new class that extends the GeneralLogEvent class.
class MyCustomEvent extends GeneralLogEvent { public const EVENT_CODE = '991414'; public const EVENT_KEY = 'my_custom_event'; }
Usage in other frameworks
This package is framework agnostic. However, there are other packages that provide a framework specific implementation.
- Laravel: minvws/nl-rdo-laravel-logging
- Symfony: minvws/nl-rdo-php-audit-logger-bundle
Contributing
If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub repository of this package.
License
This package is open-source and released under the European Union Public License version 1.2. You are free to use, modify, and distribute the package in accordance with the terms of the license.
Part of iCore
This package is part of the iCore project.
Footnotes
-
This event is used for all account changes. The event code is used to identify the type of change. Set this with the
->withEventCode()method. ↩
minvws/audit-logger 适用场景与选型建议
minvws/audit-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.44k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「Audit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 minvws/audit-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 minvws/audit-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 minvws/audit-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
A Zend Framework module that sets up Monolog for logging in applications.
PB Web Media Audit Bundle for Symfony
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
统计信息
- 总下载量: 51.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: EUPL-1.2
- 更新时间: 2025-06-12