dwi-wijonarko/laravel-activity-logger 问题修复 & 功能扩展

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

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

dwi-wijonarko/laravel-activity-logger

最新稳定版本:1.1.0

Composer 安装命令:

composer require dwi-wijonarko/laravel-activity-logger

包简介

Simple Laravel package for logging user activities with automatic tracking

README 文档

README

Latest Version on Packagist Total Downloads

A simple and elegant Laravel package for logging user activities. Track all user actions including create, update, and delete operations with automatic logging.

Features

  • 🚀 Auto Logging: Automatically log model events using a simple trait
  • 📝 Manual Logging: Fluent interface for custom activity logging
  • 🔍 Query Filters: Easy querying by user, model, date range, etc.
  • 💾 Detailed Tracking: Stores old and new values, IP address, user agent
  • ⚙️ Configurable: Customize table name, ignored attributes, retention period
  • 🎯 Laravel 9-12 Support: Compatible with latest Laravel versions

Installation

Install via Composer:

composer require dwi-wijonarko/laravel-activity-logger

Publish the config file:

php artisan vendor:publish --tag=activity-logger-config

Run migrations:

php artisan migrate

Usage

Automatic Logging with Trait

Add the Loggable trait to any model you want to track:

use DwiWijonarko\ActivityLogger\Traits\Loggable;

class Post extends Model
{
    use Loggable;
    
    // Optional: customize log name
    protected $logName = 'posts';
}

Now all created, updated, and deleted events will be automatically logged!

Manual Logging

Use the activity() helper for custom logs:

// Simple logging
activity()->log('User viewed dashboard');

// With subject model
activity()
    ->performedOn($post)
    ->log('Post was published');

// With causer (authenticated user by default)
activity()
    ->causedBy($user)
    ->performedOn($post)
    ->log('Admin featured this post');

// With custom properties
activity()
    ->performedOn($post)
    ->withProperties(['custom' => 'data', 'reason' => 'Featured'])
    ->log('Post was featured');

// With log name (for categorization)
activity()
    ->inLog('admin')
    ->performedOn($post)
    ->log('Post was reviewed');

Querying Activity Logs

use DwiWijonarko\ActivityLogger\Models\Activity;

// Get all activities
$activities = Activity::all();

// Get activities for a specific model
$postActivities = Activity::forSubject($post)->get();

// Get activities by a specific user
$userActivities = Activity::forCauser($user)->get();

// Filter by log name
$adminLogs = Activity::inLog('admin')->get();

// Filter by date range
$recentActivities = Activity::between(
    now()->subDays(7),
    now()
)->get();

// Combine filters
$activities = Activity::forCauser($user)
    ->inLog('posts')
    ->between(now()->subMonth(), now())
    ->latest()
    ->paginate(20);

Accessing Log Data

$activity = Activity::first();

// Get the user who performed the action
$user = $activity->causer;

// Get the model that was affected
$subject = $activity->subject;

// Get changes (for update events)
$changes = $activity->getChanges();
$oldValues = $activity->getOldValues();

// Access properties
$customData = $activity->properties;

Relationship Usage

// In your User model
public function activities()
{
    return $this->morphMany(Activity::class, 'causer');
}

// In your Post model
public function activities()
{
    return $this->morphMany(Activity::class, 'subject');
}

// Usage
$user->activities; // All activities by this user
$post->activities; // All activities on this post

Configuration

Edit config/activity-logger.php:

return [
    // Enable/disable logging
    'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),
    
    // Table name
    'table_name' => 'activity_logs',
    
    // Auto-delete logs older than X days
    'delete_after_days' => 90,
    
    // Attributes to ignore when logging updates
    'ignore_attributes' => [
        'password',
        'remember_token',
        'created_at',
        'updated_at',
    ],
    
    // Log authentication events
    'log_auth' => true,
];

Examples

Blog Post Management

class Post extends Model
{
    use Loggable;
    
    protected $logName = 'content';
}

// Activities are automatically logged
$post = Post::create(['title' => 'My Post', 'content' => '...']);
// Logs: "Created Post"

$post->update(['title' => 'Updated Title']);
// Logs: "Updated Post" with old and new values

$post->delete();
// Logs: "Deleted Post"

Custom Admin Actions

// When admin approves content
activity()
    ->inLog('admin')
    ->performedOn($post)
    ->causedBy($admin)
    ->withProperties(['status' => 'approved', 'notes' => 'Quality content'])
    ->log('Content approved by admin');

User Activity Timeline

// Display user's activity timeline
$activities = Activity::forCauser($user)
    ->latest()
    ->paginate(20);

foreach ($activities as $activity) {
    echo "{$activity->created_at}: {$activity->description}";
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on recent changes.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Security

If you discover any security-related issues, please email dwi.wijonarko@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-11