yungts97/laravel-user-activity-log

yungts97开发的一个简单的Laravel包,用于轻松监控您的Laravel应用程序用户活动。

v1.4.2 2024-08-27 12:47 UTC

This package is auto-updated.

Last update: 2024-09-27 13:06:22 UTC


README

Logo mark of Laravel User Activity Log

Laravel用户活动日志

yungts97/laravel-user-activity-log是一个为Laravel 8.x提供的包,它提供易于使用的功能来记录您的Laravel应用程序用户的活动。它提供对模型事件的自动记录,无需复杂的操作。所有活动都将存储在logs表中。

📦 环境要求

PHP: ^8.0

Laravel: ^8.x - ^9.0

🚀 安装

您可以通过Composer安装此包

composer require yungts97/laravel-user-activity-log

该包将自动注册服务提供者。

之后,我们还需要执行以下命令来完成安装。

php artisan user-activity-log:install

✨ 如何使用?

此包非常简单易用。您需要做的唯一一件事是在您的模型类中添加Loggable特性。

use Illuminate\Database\Eloquent\Model;
use Yungts97\LaravelUserActivityLog\Traits\Loggable;

class Post extends Model
{
    use Loggable; // add it in here
    ...
}

如果您有大量的模型,可能会有些麻烦。您可以为您的模型创建一个基类,并在基模型中添加Loggable特性。

# BaseModel.php
use Illuminate\Database\Eloquent\Model;
use Yungts97\LaravelUserActivityLog\Traits\Loggable;

class BaseModel extends Model
{
    use Loggable;
}

# PostModel.php
class Post extends BaseModel
{
    ...
}

如果您不希望对基模型的任何子类进行日志记录,您可以在模型中添加SkipLogging特性来跳过记录。

use Yungts97\LaravelUserActivityLog\Traits\SkipLogging;

class Post extends BaseModel
{
    use SkipLogging;
    ...
}

有时,您可能不想在日志中保存模型的一些属性。您可以在模型中添加$log_hidden属性。

use Yungts97\LaravelUserActivityLog\Traits\SkipLogging;

class Post extends BaseModel
{
    public $log_hidden = ['created_at', 'description'];
    ...
}

您可以使用Yungts97\LaravelUserActivityLog\Models\Log模型检索所有活动。

Log::all();

但是,您可以通过这种方式从模型中获取活动日志。

$post->logs; // get all model's logs
$post->log; // get the latest model's log
$post->logs()->where('log_type', 'edit')->get(); // get filtered logs

您可以为edit事件日志指定模式。目前有两种模式:simple/full。默认模式是full

# config/user-activity-log.php

# only can choose either one of them
'mode' => 'full',   # the 'full' mode record everything
'mode' => 'simple', # the 'simple' mode only record the modified columns

⚙️ 配置

您可以在config/user-activity-log.php中更改此包的配置。

return [
    # add your own middleware here (route middleware)
    'middleware' => ['api', 'auth'],

    # user model class
    'user_model' =>  "App\Models\User",

    # exclude tables for filter option
    'exclude_tables' => [
        'logs',
        'migrations',
        'failed_jobs',
        'password_resets',
        'personal_access_tokens',
    ],

    # events to log
    'events' => [
        'create' => true,
        'edit'   => true,
        'delete' => true,
        'retrieve' => false,
        'login'  => true,
        'logout' => true
    ],

    # the mode is only for 'edit' event log
    # the 'simple' mode only record the modified columns
    # the 'full' mode record everything
    # supported mode => 'simple' / 'full'
    'mode' => 'full',

    # timezone for log date time (Change to your region time zone, or any other variation of the timezone key in .env)
    # UTC is the default the time zone being recorded.
    # define your timezone to have the accurate logs time and filtered record (Especially filtered by date time)
    'timezone' => env('APP_TIMEZONE','UTC')
];

🐣 API路由

日志过滤的可用参数

例如:http://example.com/api/logs?page=1&itemsPerPage=10&userId=517

📬 示例响应

/logs

{
    "current_page": 1,
    "data": [
        {
            "id": 77,
            "user_id": 942,
            "log_datetime": "2022-01-22T05:56:57.000000Z",
            "table_name": null,
            "log_type": "login",
            "request_info": {
                "ip": "192.121.0.56",
                "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
            },
            "data": null,
            "current_data": null,
            "humanize_datetime": "17 seconds ago",
            "user": {
                "id": 1,
                "name": "User 1",
            }
        },
    ],
    "first_page_url": "https:///api/logs?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "https:///api/logs?page=1",
    "links": [
        {
            "url": null,
            "label": "« Previous",
            "active": false
        },
        {
            "url": "https:///api/logs?page=1",
            "label": "1",
            "active": true
        },
    ],
    "next_page_url": null,
    "path": "https:///api/logs",
    "per_page": "10",
    "prev_page_url": null,
    "to": 10,
    "total": 1
}

/logs/filter-options

{
    "table_names": [
        "posts",
        "users",
    ],
    "log_types": [
        "create",
        "edit",
        "delete",
        "login",
        "logout"
    ]
}

/logs/77

{
    "id": 77,
    "user_id": 942,
    "log_datetime": "2022-01-22T05:56:57.000000Z",
    "table_name": null,
    "log_type": "login",
    "request_info": {
        "ip": "192.121.0.56",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
    },
    "data": null,
    "current_data": null,
    "humanize_datetime": "17 seconds ago",
    "user": {
        "id": 1,
        "name": "User 1",
    }
},

🎩 Artisan命令

✒️ user-activity-log:clean的选项

⚠️ 注意:默认情况下,如果没有应用任何选项,则为--day=7

📃 许可证

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