sagor/activity-log
Composer 安装命令:
composer require sagor/activity-log
包简介
A simple but powerfull package to log user activities in Laravel.
README 文档
README
A zero-configuration Laravel package to automatically log user activities, model changes, HTTP requests, and authentication events. Everything works out-of-the-box, no manual setup required.
📌 Compatibility
- Laravel: 5.x - 12.x
- PHP: 7.2 - 8.4
⚡ Features
- Automatic logging of all Eloquent model events (
created,updated,deleted,restored) - Logs user login and logout events
- Logs HTTP requests that modify data (
POST,PUT,PATCH,DELETE) - Manual logging with the
activity()helper - No configuration required
🛠 Installation and Setup (Full Process)
- Install via Composer
composer require sagor/activity-log
- Run Migration
The package automatically provides a migration. Run:
php artisan migrate
This will create a table called automatic_activity_log with the following structure:
| Column | Type | Description |
|---|---|---|
| id | bigint | Primary key |
| causer_type | string | Type of the user or system causing the action (polymorphic) |
| causer_id | bigint | ID of the user or system causing the action (polymorphic) |
| action_type | string | Type of activity (e.g., created, updated, deleted) |
| description | longText | Description of the activity |
| affected_model_type | string | Type of the affected model (polymorphic) |
| affected_model_id | bigint | ID of the affected model (polymorphic) |
| ip_address | string | IP address of the user performing the action |
| user_agent | text | User agent of the requester |
| url | string | URL where the action occurred |
| method | string | HTTP method used |
| created_at | timestamp | Log creation time |
| updated_at | timestamp | Log update time |
- How It Works
The package automatically listens to Laravel events:
eloquent.*→ logs all model events (created,updated,deleted,restored)Illuminate\Auth\Events\Login&Logout→ logs authentication eventsIlluminate\Foundation\Http\Events\RequestHandled→ logsPOST,PUT,PATCH,DELETErequests
All activities are stored automatically in the automatic_activity_log table. No manual calls needed.
✍️ Manual Logging Example
If you want to log custom actions, you can use the activity() helper:
activity() ->causedBy(auth()->user()) // The user performing the action ->performedOn($someModel) // The affected model ->withType('custom') // Custom type (optional) ->log('A custom action was performed.'); // Description
Example:
// Log a manual update for a post activity() ->causedBy(auth()->user()) ->performedOn($post) ->withType('custom') ->log('Updated a post manually.');
📚 Full Usage Flow
- Install the package with Composer
- Run
php artisan migrateto create the log table - Automatically track:
- All model changes
- User login/logout events
- HTTP requests that modify data
- Optionally, log custom actions using
activity()helper - Check the
automatic_activity_logtable for logs
No additional configuration is needed. Just install, migrate, and it works.
Route with controller
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
Route::get('/activity-log', function (Request $request) {
$query = DB::table('automatic_activity_log')->orderBy('created_at', 'desc');
// Apply search filter if provided
if ($request->filled('search')) {
$search = $request->search;
$query->where('description', 'like', "%{$search}%")
->orWhere('action_type', 'like', "%{$search}%")
->orWhere('causer_type', 'like', "%{$search}%")
->orWhere('affected_model_type', 'like', "%{$search}%");
}
// Paginate results
$logs = $query->paginate(10)->withQueryString();
return view('activity-log', compact('logs', 'request'));
});
Blade with Filter and pagination
@extends('layouts.app')
@section('title', 'Activity Log')
@section('content')
<div class="container">
<h1 class="mb-4">Activity Logs</h1>
<!-- Search Form -->
<form method="GET" action="{{ url('/activity-log') }}" class="mb-3">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search..." value="{{ $request->search }}">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Causer</th>
<th>Action Type</th>
<th>Description</th>
<th>Affected Model</th>
<th>IP Address</th>
<th>User Agent</th>
<th>URL</th>
<th>Method</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
@forelse ($logs as $log)
<tr>
<td>{{ $log->id }}</td>
<td>{{ $log->causer_type }} #{{ $log->causer_id }}</td>
<td>{{ $log->action_type }}</td>
<td>{{ $log->description }}</td>
<td>{{ $log->affected_model_type }} #{{ $log->affected_model_id }}</td>
<td>{{ $log->ip_address }}</td>
<td>{{ $log->user_agent }}</td>
<td>{{ $log->url }}</td>
<td>{{ $log->method }}</td>
<td>{{ $log->created_at }}</td>
</tr>
@empty
<tr>
<td colspan="10" class="text-center">No logs found.</td>
</tr>
@endforelse
</tbody>
</table>
<!-- Pagination Links -->
<div class="d-flex justify-content-center">
{{ $logs->links() }}
</div>
</div>
@endsection
🛡 License
MIT License. See the LICENSE file for details.
👤 Author
Md Sagor Hossain Email: sagorhassain4@gmail.com GitHub: https://github.com/moh-sagor/automatic-activity-log
🚀 Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/AmazingFeature - Commit your changes:
git commit -m 'Add some AmazingFeature' - Push to the branch:
git push origin feature/AmazingFeature - Open a Pull Request
🎯 Support
If you like this package, give it a ⭐ on GitHub!
sagor/activity-log 适用场景与选型建议
sagor/activity-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「git」 「github」 「laravel」 「artisan」 「activitylog」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sagor/activity-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sagor/activity-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sagor/activity-log 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handles basic OAuth/OAuth2 authentication along with classes for common services
Easily manage git hooks in your composer config
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
Nagios plugin to verify a git repository.
Laravel package for the simplification and integration of many of your users most wanted login providers. Installation is a breeze. Never worry about OAuth again.
GitHub Webhook Listener with plugin-based API for creating your own triggerered actions
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-14