mattyeend/logging
Composer 安装命令:
composer require mattyeend/logging
包简介
A comprehensive logging library for Laravel applications.
README 文档
README
A Laravel package for logging application events into a database table. This package provides an extensible model, a database migration, and helper methods for recording user and system actions in a structured and searchable format.
Features
- Action Logging: Easily log predefined or custom actions with optional metadata.
- User Relationships: Track the user performing the action (
logged_in_user_id) and any user related to the action (related_to_user_id). - Structured Data: Store additional data as JSON for enhanced flexibility.
- Predefined Actions: Common actions like login, logout, user creation, and more are included.
Installation
-
Require the package Run the following command:
composer require MattYeend/Logging -
Publish the
log.phpmodel Runphp artisan vendor:publish --tag=logging-model -
Run Migrations Run the migrations to create the
logstable:php artisan migrate
Usage
Log an Action
Use the Log model to log actions. The predefined constants simplify common actions
use App\Models\Log;
// Log a user login event
Log::log(Log::ACTION_LOGIN, ['ip' => request()->ip()], auth()->id());
Log Custom Actions
You can log actions not predefined in the package:
Log::log(99, ['custom_key' => 'custom_value'], auth()->id(), $relatedUserId);
Model Relationships
Retrieve the user who performed the action or the user related to it:
$log = Log::find(1);
// Logged-in user
$loggedInUser = $log->loggedInUser;
// Related user
$relatedUser = $log->relatedToUser;
Predefined Actions
| Constant | Value | Description |
|---|---|---|
ACTION_LOGIN |
1 | User logged in |
ACTION_LOGOUT |
2 | User logged out |
ACTION_CREATE_USER |
3 | Created a new user |
ACTION_UPDATE_USER |
4 | Updated a user |
ACTION_DELETE_USER |
5 | Deleted a user |
ACTION_SHOW_USER |
6 | Viewed a user profile |
ACTION_WELCOME_EMAIL_SENT |
7 | Sent a welcome email |
ACTION_CONFIRM_PASSWORD |
8 | Confirmed password |
ACTION_FORGOT_PASSWORD |
9 | Initiated password reset |
ACTION_REGISTER_USER |
10 | Registered a new user |
ACTION_LOGIN_FAILED |
11 | Login failed |
ACTION_LOGIN_PASSWORD_FAILED |
12 | Login via password failed |
ACTION_LOGIN_EMAIL_FAILED |
13 | Login via email failed |
ACTION_LOGIN_USERNAME_FAILED |
14 | Login via username failed |
ACTION_LOGIN_SUCCESS |
15 | Login success |
ACTION_RESET_PASSWORD |
16 | Reset password |
ACTION_RESET_EMAIL |
17 | Reset email |
ACTION_RESET_USERNAME |
18 | Reset username |
ACTION_VERIFY_USER |
19 | Verified user email |
ACTION_PASSWORD_CHANGED |
20 | Changed password |
ACTION_MFA_ENABLED |
21 | Enabled multi-factor auth |
ACTION_MFA_DISABLED |
22 | Disabled multi-factor auth |
ACTION_PROFILE_UPDATED |
23 | Updated user profile |
ACTION_EMAIL_UPDATED |
24 | Updated email address |
ACTION_ROLE_ASSIGNED |
25 | Assigned a role to a user |
ACTION_PERMISSION_GRANTED |
26 | Granted permissions to user |
ACTION_PERMISSION_REVOKED |
27 | Revoked user permissions |
ACTION_GENERAL_ERROR |
28 | General error |
ACTION_FOUR_HUNDRED_ERROR |
29 | Four hundred error |
ACTION_FIVE_HUNDRED_ERROR |
30 | Five hundred error |
ACTION_CLEAR_CACHE |
31 | Clear Cache |
Customisation
Extend the Logger Model
Add additional functionality by extending the Log model in your application.
Troubleshooting
-
Migration Not Found Ensure the
LoggerServiceProvideris loading migrations by verifying the path in:src/LoggerServiceProvider.php:$this->loadMigrationsFrom(__DIR__ . '/Database/Migrations'); -
Data Validation Issues Ensure
datapassed toLog::log()is eithernullor an array. Example:Log::log(Log::ACTION_LOGIN, ['key' => 'value'], auth()->id());
Testing
To run tests, follow the next steps:
- Install dependencies:
composer install
- Run the tests:
composer test
License
This package is licensed under the MIT License.
Contributing
Feel free to fork the repository and submit pull requests for improvements or new features!
mattyeend/logging 适用场景与选型建议
mattyeend/logging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「package」 「logger」 「Audit」 「laravel」 「tracking」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mattyeend/logging 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mattyeend/logging 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mattyeend/logging 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module that sets up Monolog for logging in applications.
Workflow logger
HTTP request logger middleware for Laravel
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
统计信息
- 总下载量: 94
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-11