uatthaphon/laravel-activity-monitor

记录以监控您网站的活动

0.1.8 2018-02-20 23:39 UTC

This package is auto-updated.

Last update: 2024-09-29 04:09:03 UTC


README

活动监控日志是一个用于监控用户活动和Eloquent模型事件活动的日志记录器。

注意:此包适用于laravel 5.5+,需要php 7.0+

设置

将包依赖添加到您的项目中

composer require uatthaphon/laravel-activity-monitor

由于此包是为laravel 5.5+构建的,我使用了自动发现,因此我们不再需要在config\app.php中添加服务提供者。

运行发布以获取activity_monitors表的数据库迁移

php artisan vendor:publish --tag=migrations

发布后,我们可以通过运行迁移来创建表

php artisan migrate

用法

日志记录器

此包提供了两个别名AMLogAMView,方便我们保存日志和查看日志。

我们也不需要将这两个别名添加到config/app.php中,它们已经通过自动发现为我们添加了。

示例:记录用户更新了他们的帖子。

use AMLog;

$post = Post::where('user_id', $id)->firstOrFail();
$post->body = 'update body content';
$post->save();

AMLog::logName('custom log name')               // Declare log name
    ->description('user updated post content')  // Log description
    ->happenTo($post)                           // Model of the event happen to
    ->actBy(\Auth::user())                      // Model that cause this event
    ->meta(['key'=>'value'])                    // Additional pieces of information
    ->save();                                   // Let's Save the log

AMlog还为我们准备了一些日志名称,以便我们容易使用 => debugerrorfatalinfowarning

use AMLog;
...
AMLog::debug('some debug description')->save();

AMLog::error('some error description')->save();

AMLog::fatal('some fatal description')->save();

AMLog::info('some info description')->save();

AMLog::warning('some warning description')->save();

就是这样 🎶

Eloquent模型事件日志

createdupdateddeleted时,轻松记录Eloquent模型活动。

设置好包后,将ModelEventActivity特性添加到您的模型中。

namespace App\Models;

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;
    ...
}

此功能将通过设置protected static $loggable来记录应用程序中的更改,告诉记录器哪些属性应该被记录。

注意:它不会记录使用数据库默认值的属性... 除非您自行添加值到属性

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $loggable = ['title', 'description']
}

如果title记录有变化,它将只记录activity_monitors表中的标题字段

{"title": "has some change"}

我们可以通过设置protected static $eventsToLog来自定义应记录哪些Eloquent事件。

在下面的示例中,仅为此模型记录created事件

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $eventsToLog = ['created']
}

我们可以通过向您的模型添加以下内容来向每个事件添加我们的元数据

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $createdEventMeta = ['create key' => 'create value'];

    protected static $updatedEventMeta = ['update key' => 'update value'];

    protected static $deletedEventMeta = ['deletd key' => 'delete value'];

    ...
}

查看日志

我们可以使用AMView来获取我们的日志,它将返回ActivityMonitor对象

请看下面的示例

use AMView;

...

// Get all
AMView::all();                                    // get all the logs
AMView::get();                                    // also act the same as all()

// With conditions
AMView::logName('your_log_name')                  // get by log name
    ->limit(5)                                    // limit resutls
    ->sortBy('desc')                              // sort By desc or asc
    ->get();

// Get from multiple log names
AMView::logName('info', 'updated')->get();
AMView::logName(['info', 'updated'])->get();


// Get all specific lastest post log From current user
$user = \Auth::user();
$post = $user->post()->last($user);
AMView::happenTo($post)->ActBy($user)->get();

// Or call from providings log name function
AMView::debug()->all();

AMView::error()->all();

AMView::fatal()->all();

AMView::info()->all();

AMView::warning()->all();

...

尝试一下。它将返回ActivityMonitor模型的集合

use AMView;

...

$am = AMView::info()->all()->last();

$am->log_name;                          // Get log name
$am->description;                       // Get description
$am->agent;                             // Get user browser agent
$am->ip;                                // Get user ip address

$traces = $am->traces;                  // Get traces

foreach ($traces as $key => $value) {
    // do something
}

$meta = $am->meta;                      // Get you custom meta data

foreach ($meta as $key => $value) {
    // do something
}

...

在特定模型中查看日志

我们可以将ActivityMonitor添加到我们的模型中

...
use Uatthaphon\ActivityMonitor\Traits\ActivityMonitorRelations;

class User extends Authenticatable
{
  use ActivityMonitorRelations;

  ...
}

现在我们可以使用activity()多态关系

// Get all activity records of the current user
\Auth::user()->activity()->get();

// Retrived with more specific
// By tell with model record user was interact with
$user = \Auth::user();
$user->activity()->happenTo($user->posts()-last())->get();

// Use the providing log name function
$user->activity()->info()->get();

// Use the providing log name with specific fom model togetter
$user->activity()
    ->info()
    ->happenTo($user->posts()-last())
    ->get();