rajmundtoth0/laravel-auditing-elasticsearch-driver
Composer 安装命令:
composer require rajmundtoth0/laravel-auditing-elasticsearch-driver
包简介
An Elasticsearch driver for the owen-it/laravel-auditing package.
README 文档
README
Laravel Auditing Elasticsearch Driver
Elasticsearch driver for Laravel Auditing, with support for both classic indices and Elasticsearch data streams.
Highlights
- Index or data stream storage mode (
index/data_stream) - Optional ILM lifecycle policy + index template management for data streams
- Queue-aware indexing
- Typed configuration and strict static analysis support
- CI coverage for feature and integration test suites
Requirements
| Dependency | Supported |
|---|---|
| PHP | >=8.2 |
| Laravel | ^11 | ^12 |
| elasticsearch/elasticsearch | ^8.0 | ^9.0 |
| owen-it/laravel-auditing | ^13.0 | ^14.0 |
Installation
composer require rajmundtoth0/laravel-auditing-elasticsearch-driver
If you need to publish package config:
php artisan vendor:publish --provider="rajmundtoth0\\AuditDriver\\ElasticsearchAuditingServiceProvider"
Set Laravel Auditing to use this driver in config/audit.php:
'driver' => rajmundtoth0\AuditDriver\Services\ElasticsearchAuditService::class,
Configuration
Use the drivers.elastic section in config/audit.php:
'elastic' => [ 'hosts' => [env('AUDIT_HOST', 'http://0.0.0.0:9200')], 'userName' => env('ELASTIC_AUDIT_USER', 'elastic'), 'password' => env('ELASTIC_AUDIT_PASSWORD', 'a_very_strong_password'), 'useBasicAuth' => (bool) env('AUDIT_BASIC_AUTH', true), 'useCaCert' => (bool) env('AUDIT_USE_CERT', true), 'certPath' => env('AUDIT_CERT_PATH', ''), 'index' => env('AUDIT_INDEX', 'laravel_auditing'), 'storageMode' => env('AUDIT_STORAGE_MODE', 'index'), // index|data_stream 'definitions' => [ 'settings' => [ 'path' => env('AUDIT_SETTINGS_PATH', ''), ], 'mappings' => [ 'path' => env('AUDIT_MAPPINGS_PATH', ''), ], 'lifecyclePolicy' => [ 'path' => env('AUDIT_LIFECYCLE_POLICY_PATH', ''), ], ], 'dataStream' => [ 'templateName' => env('AUDIT_DATA_STREAM_TEMPLATE_NAME', env('AUDIT_INDEX', 'laravel_auditing').'_template'), 'indexPattern' => env('AUDIT_DATA_STREAM_INDEX_PATTERN', env('AUDIT_INDEX', 'laravel_auditing').'*'), 'templatePriority' => (int) env('AUDIT_DATA_STREAM_TEMPLATE_PRIORITY', 100), 'lifecyclePolicyName' => env('AUDIT_DATA_STREAM_LIFECYCLE_POLICY', ''), 'pipeline' => env('AUDIT_DATA_STREAM_PIPELINE', ''), ], 'singleWriteRetry' => [ 'enabled' => (bool) env('AUDIT_SINGLE_WRITE_RETRY_ENABLED', true), 'maxAttempts' => (int) env('AUDIT_SINGLE_WRITE_RETRY_MAX_ATTEMPTS', 3), 'initialBackoffMs' => (int) env('AUDIT_SINGLE_WRITE_RETRY_INITIAL_BACKOFF_MS', 100), 'maxBackoffMs' => (int) env('AUDIT_SINGLE_WRITE_RETRY_MAX_BACKOFF_MS', 2000), 'backoffMultiplier' => (float) env('AUDIT_SINGLE_WRITE_RETRY_BACKOFF_MULTIPLIER', 2.0), 'jitterMs' => (int) env('AUDIT_SINGLE_WRITE_RETRY_JITTER_MS', 25), ], ],
JSON Definitions
Default JSON definitions are stored in:
resources/elasticsearch/settings.jsonresources/elasticsearch/mappings.jsonresources/elasticsearch/lifecycle-policy.json
mappings.json defines old_values and new_values as dynamic objects, so model-specific audit keys can be indexed without predefined fields.
The driver resolves each definition in this order:
- File path from
definitions.*.path - Package default JSON file in
resources/elasticsearch/
File path override example:
'definitions' => [ 'settings' => [ 'path' => base_path('infra/elasticsearch/settings.json'), ], 'mappings' => [ 'path' => base_path('infra/elasticsearch/mappings.json'), ], 'lifecyclePolicy' => [ 'path' => base_path('infra/elasticsearch/lifecycle.json'), ], ],
Storage Mode Behavior
indexmode: setup creates index + write alias.data_streammode: setup creates/updates template and optional ILM policy; Elasticsearch auto-creates the data stream on first write.
Note: in data_stream mode, the driver auto-populates @timestamp if missing.
Single-Write Retries
Single document writes use retries with exponential backoff for transient failures (408, 429, 5xx, node-unavailable).
Retry timing values are configured through singleWriteRetry.* in config/audit.php.
maxAttempts: total attempts, including the first callinitialBackoffMs: delay before first retrybackoffMultiplier: exponential factor per retrymaxBackoffMs: upper bound for delayjitterMs: random+/-jitter to avoid synchronized retry spikes
In data_stream mode, op_type=create conflicts (409) are treated as success to keep retries idempotent.
Setup Command
Run once after configuration changes:
php artisan es-audit-log:setup
Lifecycle Policy Example
If you want the package to create/update an ILM policy, set lifecyclePolicyName and provide the policy through definitions.lifecyclePolicy (JSON or file path):
'dataStream' => [ 'lifecyclePolicyName' => 'audits-hot-delete', ], 'definitions' => [ 'lifecyclePolicy' => [ 'path' => base_path('infra/elasticsearch/lifecycle.json'), ], ],
Lifecycle definitions are validated at boot and must contain policy.phases as a non-empty object.
Usage
Add OwenIt\Auditing\Auditable to your model as usual.
To read Elasticsearch audit logs from your model:
$model->audit_log$model->elasticsearchAuditLog($page, $pageSize, $sort)
These methods are provided by ElasticSearchAuditable.
Queue Support
If drivers.queue.enabled is true, audit documents are queued and indexed asynchronously.
Testing
composer test
composer analyse
Integration tests (against real Elasticsearch):
AUDIT_RUN_INTEGRATION_TESTS=true AUDIT_INTEGRATION_HOST=http://localhost:9200 composer test-integration
Contributing
Open pull requests against the master branch.
rajmundtoth0/laravel-auditing-elasticsearch-driver 适用场景与选型建议
rajmundtoth0/laravel-auditing-elasticsearch-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97.64k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「elasticsearch」 「Audit」 「observer」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rajmundtoth0/laravel-auditing-elasticsearch-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rajmundtoth0/laravel-auditing-elasticsearch-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rajmundtoth0/laravel-auditing-elasticsearch-driver 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module that sets up Monolog for logging in applications.
simple api library.
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
统计信息
- 总下载量: 97.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-26