spatie / activitylog
Requires
- php: >=5.4.0
- illuminate/support: 5.*
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.0
README
版本过时警告
此包于2016-06-28被废弃。请使用 laravel-activitylog 代替。
描述
此Laravel 5包提供了一个非常易于使用的解决方案来记录您的Laravel 5应用的用户活动。所有活动都将记录在数据库表中。可选地,活动也可以记录到默认的Laravel日志处理程序。
Spatie是位于比利时安特卫普的一家网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 。
注意
如果您正在使用Laravel 4,请查看此包的0.3.0版本。
明信片软件
您可以自由使用此包(它遵循 MIT许可),但如果它进入您的生产环境,您需要向我们寄一张来自您家乡的明信片,说明您正在使用我们的哪个包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
最好的明信片将被发布在我们网站的开源页面上。
安装
此包可以通过Composer安装。
composer require spatie/activitylog
必须注册此服务提供者。
// config/app.php 'providers' => [ '...', 'Spatie\Activitylog\ActivitylogServiceProvider', ];
您还需要发布并运行迁移来创建数据库表。
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate
Activitylog还附带了一个外观,它提供了一种简单的方法来调用它。
// config/app.php 'aliases' => [ ... 'Activity' => 'Spatie\Activitylog\ActivitylogFacade', ];
可选地,您可以发布此包的配置文件。
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
配置将被写入 config/activitylog.php
。提供的选项是自我解释的。
使用
手动记录
记录一些活动非常简单。
//at the top of your file you should import the facade. use Activity; ... /* The log-function takes two parameters: - $text: the activity you wish to log. - $user: optional can be an user id or a user object. if not proved the id of Auth::user() will be used */ Activity::log('Some activity that you wish to log');
传递给函数的字符串将被写入数据库表,并附带时间戳、用户的IP地址和用户代理。
记录模型事件
此包可以记录您的模型事件。为此,您的模型必须使用 LogsActivity
-trait 并实现 LogsActivityInterface
。
use Spatie\Activitylog\LogsActivityInterface; use Spatie\Activitylog\LogsActivity; class Article implements LogsActivityInterface { use LogsActivity; ...
该接口期望您实现 getActivityDescriptionForEvent
-函数。
以下是一个可能的实现示例。
/** * Get the message that needs to be logged for the given event name. * * @param string $eventName * @return string */ public function getActivityDescriptionForEvent($eventName) { if ($eventName == 'created') { return 'Article "' . $this->name . '" was created'; } if ($eventName == 'updated') { return 'Article "' . $this->name . '" was updated'; } if ($eventName == 'deleted') { return 'Article "' . $this->name . '" was deleted'; } return ''; }
此函数的结果将被记录,除非结果为空字符串。
使用前置处理器
如果您想在特定条件下禁用日志记录,例如针对特定用户,请创建一个实现 Spatie\Activitylog\Handlers\BeforeHandlerInterface
的类,在您的应用命名空间中。
此接口定义了一个 shouldLog()
方法,您可以在其中编写任何自定义逻辑来决定是否忽略日志记录。您必须返回 true
以记录调用。
将命名空间类名添加到配置文件中的 beforeHandler
字段。
'beforeHandler' => '\App\Handlers\BeforeHandler',
例如,这个回调类可以如下所示以禁用ID为1的用户日志记录:
<?php namespace App\Handlers; use Spatie\Activitylog\Handlers\BeforeHandlerInterface; class BeforeHandler implements BeforeHandlerInterface { public function shouldLog($text, $userId) { if ($userId == 1) return false; return true; } }
检索记录条目
所有事件都将记录在 activity_log
表中。此包提供了一个Eloquent模型来处理此表。您可以使用所有熟悉的Eloquent方法。以下是如何获取最后100个活动及其关联用户的方法。
use Spatie\Activitylog\Models\Activity; $latestActivities = Activity::with('user')->latest()->limit(100)->get();
清理日志
随着时间的推移,您的日志将增长。要清理数据库表,可以运行此命令
Activity::cleanLog();
默认情况下,将删除超过2个月的记录。月份的数量可以在包的配置文件中修改。
贡献
请参阅 CONTRIBUTING 获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 freek@spatie.be 联系,而不是使用问题跟踪器。
致谢
关于Spatie
Spatie是位于比利时安特卫普的一家网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 。
许可证
MIT许可证(MIT)。请参阅 许可证文件 获取更多信息。