fiopay/activitylog

一个用于监控您的网站或应用程序用户的非常简单的活动记录器

2.4.3 2016-06-29 07:44 UTC

This package is not auto-updated.

Last update: 2024-09-26 18:51:30 UTC


README

Latest Version Software License Build Status SensioLabsInsight Total Downloads

EOL警告

此软件包已于2016年6月28日废弃。请使用 laravel-activitylog 代替。

描述

此Laravel 5软件包提供了一个非常易于使用的解决方案,用于记录您的Laravel 5应用程序用户的操作。所有操作都将记录在数据库表中。可选地,操作也可以记录到默认的Laravel日志处理程序。

Spatie是比利时安特卫普的一家网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

注意

如果您正在使用Laravel 4,请查看此软件包的0.3.0版本。

Postcardware

您可以使用此软件包(它是 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-特质并实现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 '';
}

此函数的结果将被记录,除非结果是空字符串。

使用before处理器。

如果您想在某些条件下禁用记录,例如对特定用户,请创建一个实现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个月前的记录将被删除。月份的数量可以在包的配置文件中修改。

贡献

有关详细信息,请参阅贡献指南

安全性

如果您发现任何安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。

致谢

关于Spatie

Spatie是比利时安特卫普的一家网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。