codemyviews/activitylog

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

v1.0.1 2016-04-06 06:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:26:17 UTC


README

Software License

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

这个包最初由Spatie创建。Spatie是一家位于比利时安特卫普的网页设计公司。您可以在他们的网站上找到他们所有开源项目的概览在这里

后来我们CodeMyViews决定修改Spatie版本以更好地满足我们的需求。您可以在我们的网站上了解我们。

安装

此包可以通过Composer安装。

composer require codemyviews/activitylog

必须注册此服务提供程序。

// config/app.php

'providers' => [
    '...',
    'CodeMyViews\Activitylog\ActivitylogServiceProvider',
];

您还需要发布并运行迁移以创建数据库表。

php artisan vendor:publish --provider="CodeMyViews\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate

Activitylog还提供了一个外观,提供了调用它的简单方法。

// config/app.php

'aliases' => [
    ...
    'Activity' => 'CodeMyViews\Activitylog\ActivitylogFacade',
];

可选地,您可以发布此包的配置文件。

php artisan vendor:publish --provider="CodeMyViews\Activitylog\ActivitylogServiceProvider" --tag="config"

配置将被写入到config/activitylog.php。提供的选项是自我解释的。

用法

手动记录

记录某些活动非常简单。

// at the top of your file, you should import the facade.
use Activity;
...
/*
  The log function takes three parameters:
      - $text: the activity you wish to log.
      - $user:  optional can be a user id or a user object.
                if not proved the id of Auth::user() will be used
      - $model: optional can be an instance of Illuminate\Database\Eloquent\Model
                if not provided, none will be used
                if provided, activity will be referencing this model
*/
Activity::log('Some activity that you wish to log');
Activity::log('User has updated a post', $user, $post);

传递给函数的字符串将写入数据库表,包括时间戳、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 '';
}

此函数的结果将被记录,除非结果为空字符串。记录的活动将引用创建它们的模型。

使用前置处理器。

如果您想在某些条件下禁用记录,例如针对特定用户,则在您的应用程序命名空间中创建一个实现CodeMyViews\Activitylog\Handlers\BeforeHandlerInterface的类。

该接口定义了一个shouldLog()方法,您可以在其中编写任何自定义逻辑以确定是否忽略记录。您必须返回true以记录调用。

将命名空间类名添加到配置文件的beforeHandler字段中

'beforeHandler' => '\App\Handlers\BeforeHandler',

例如,此回调类可能如下所示以禁用ID为1的用户的记录

<?php

namespace App\Handlers;

use CodeMyViews\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 CodeMyViews\Activitylog\Models\Activity;

$latestActivities = Activity::with('user')->latest()->limit(100)->get();

清理日志

随着时间的推移,您的日志将增长。要清理数据库表,可以运行此命令

Activity::cleanLog();

默认情况下,将删除超过2个月的记录。可以在包的配置文件中修改月份数。

安全性

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

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件