backpack/activity-log 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

backpack/activity-log

Composer 安装命令:

composer require backpack/activity-log

包简介

Activity Log for Backpack

README 文档

README

Latest Version on Packagist Total Downloads The Whole Fruit Manifesto

Ever wanted to see WHO changed WHAT and WHEN inside your app? Want to remember all the DB changes your users have done? Well... this package doesn't do that. spatie/laravel-activitylog does, and does it very well. This package adds a web interface for it, for projects using Backpack for Laravel. It gives your admin/superadmin the ability to see

  • the activities performed by certain models;
  • the activities performed on certain models;
  • and more;

How does it all work? Well:

  • when a change happens to an Eloquent model, the Spatie package will make a note of it in the database;
  • this package adds a web interface, so the admin can see the changes (aka activity log);

Preview

NOTE: The filters are a Backpack\PRO feature. If you don't have that package the filters won't be available.

Demo

Try it right now, in our online demo. Edit some entities, and check the activity logs.

Installation

In your Laravel + Backpack project, install this package:

# install this interface package:
composer require backpack/activity-log

# add a menu item for it
php artisan backpack:add-menu-content "<x-backpack::menu-item title=\"Activity Logs\" icon=\"la la-stream\" :link=\"backpack_url('activity-log')\" />"

But also, if your package didn't already have spatie/laravel-activitylog installed and set up, please follow the installation steps in their docs. We'll also copy-paste them here, for your convenience:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
php artisan migrate

Usage

Note: If your models are not configured to create activity logs yet, read the FAQ on how to log all model events. If you are unsure, then you have not configured them to log activities - read it.

ActivityLog list view

Use it to browse all activity, filter the view, search the view, etc:

Backpack ActivityLog list view

ActivityLog show view

Use it to see details about a particular activity:

Backpack ActivityLog show view

CrudController operations

If you want your CrudControllers to show links to their activities, just use one or both of the provided Backpack operations:

  • ModelActivityOperation: Displays a general button, allowing users to see a comprehensive list of all activities for that Model.
  • EntryActivityOperation: Adds line buttons to each entry in the list, enabling users to view activity logs specific to individual entries.

ModelActivityOperation - on a CrudController, display a link to that Model's activity log

Say you have a UserCrudController. If you want a new button to show up next to the Add User button, that will take you to all of the activities of all Users, then use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation on your UserCrudController.

<?php

namespace App\Http\Controllers\Admin;

...

class ArticleCrudController extends CrudController
{
    ...
+    use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation;
    ...

EntryActivityOperation - on a CrudController, display a link to each entry's activity log

Say you have a UserCrudController. If you want a new button to show up next each entry, that will take you to all of the activities of that entry, then use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation on your UserCrudController.

<?php

namespace App\Http\Controllers\Admin;

...

class ArticleCrudController extends CrudController
{
    ...
+    use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation;
    ...

Linking options - Subject / Causer

ModelActivityOperation and EntryActivityOperation buttons will point to the Activity log page with the current model as the subject. If your model is a causer (the one who performed the action, most of the times User model) you can set the operation options to causer.

class ArticleCrudController extends CrudController
{
    ...
    public function setup()
    {
        ...
+        CRUD::set('activity-log.options', ActivityLogEnum::CAUSER);
    }

FAQ

What gets logged by default?

By default, the nothing gets logged. Please configure spatie/laravel-activitylog to do the logging according to your needs. See the question below for the most common use case.

How to log model events (created, updated, deleted etc)

Note: this is NOT a feature that is provided by this package. It's provided by spatie/laravel-activitylog. But we try to help document the most common use case we have found, so it's easier for you to do it.

You want a new Activity registered, whenever a model is created, updated, deleted etc? So that there's a record of WHO did WHAT and WHEN it happened? Here's how you can set up spatie/laravel-activitylog to log all model events.

Step 1. Create a new model trait at App\Models\Traits\LogsActivity.php with the following content:

<?php

namespace App\Models\Traits;

use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity as OriginalLogsActivity;

trait LogsActivity
{
    use OriginalLogsActivity;

    /**
     * Spatie Log Options
     * By default will log only the changes between fillables
     *
     * @return LogOptions
     */
    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()->logAll()->logOnlyDirty();
    }
}

Step 2. Use that trait on all Models where you want all events logged:

<?php

namespace App\Models;

+use App\Models\Traits\LogsActivity;
use Backpack\CRUD\app\Models\Traits\CrudTrait;

class Article extends Model
{
    use CrudTrait;
+   use LogsActivity;
    ...

Notice that this trait extends the default Spatie\Activitylog\Traits\LogsActivity and defines the getActivitylogOptions() method providing some reasonable defaults. If you want to customize, see details here and options here.

How do you customize what gets logged?

To customize the logged information, you can override the getActivitylogOptions method in your models.

public function getActivitylogOptions(): LogOptions
{
    return LogOptions::defaults()
        ->logFillable()
        ->logOnlyDirty();
}

Can I customize the logs even further?

Yes, you can! The Spatie laravel-activitylog package offers a range of advanced customization options. For detailed insights into these options, check out the official Log Options documentation by Spatie, at https://spatie.be/docs/laravel-activitylog/v4/api/log-options

I am using backpack/permission-manager so I don't have access to UserCrudController

If you haven't customized your UserCrudController yet, you can achieve this by binding the Backpack controller to your custom controller. In your App\Providers\AppServiceProvider.php:

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(
            \Backpack\PermissionManager\app\Http\Controllers\UserCrudController::class,
            \App\Http\Controllers\Admin\UserCrudController::class
        );

Then in your App\Http\Controllers\Admin\UserCrudController.php you can do something like:

use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as OriginalUserCrudController;

class UserCrudController extends OriginalUserCrudController
{
    use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation;
    use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation;
}

Security

If you discover any security related issues, please email cristian.tabacitu@backpackforlaravel.com instead of using the issue tracker.

Credits

License

This project was released under MIT License, so you can install it on top of any Backpack & Laravel project. Please see the license file for more information.

backpack/activity-log 适用场景与选型建议

backpack/activity-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 101.31k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2022 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「backpack」 「activitylog」 「Backpack for Laravel」 「Backpack Addon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 backpack/activity-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 backpack/activity-log 我们能提供哪些服务?
定制开发 / 二次开发

基于 backpack/activity-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 101.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 7
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-21