motakabbir/activity-logger 问题修复 & 功能扩展

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

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

motakabbir/activity-logger

Composer 安装命令:

composer require motakabbir/activity-logger

包简介

A powerful Laravel package for automatically tracking model changes and user activities with IP tracking

README 文档

README

A powerful and easy-to-use Laravel package for automatically tracking model changes and user activities within your application. This package provides automatic model tracking and manual activity logging capabilities.

Requirements

  • PHP 7.4 or higher
  • Laravel 8.0 or higher
  • MySQL 5.7+ or PostgreSQL 9.6+

Installation

To install the Activity Logger Package, you can use Composer:

composer require loctracker/activity-logger

Configuration

After installation, publish the configuration and migrations:

php artisan vendor:publish --provider="Loctracker\ActivityLogger\ActivityLoggerServiceProvider"

Then run the migrations:

php artisan migrate

Configuration Options

The package can be configured through the config/activity-logger.php file. Here are all available options:

return [
    // The logging channel to use for activity logs
    'log_channel' => env('ACTIVITY_LOG_CHANNEL', 'stack'),

    // Log level for activities
    'log_level' => env('ACTIVITY_LOG_LEVEL', 'info'),

    // The model used for storing activity logs
    'activity_model' => \Loctracker\ActivityLogger\Models\ActivityLog::class,

    // Database table name for activity logs
    'table_name' => 'activity_logs',

    // Routes that should not be logged
    'ignore_routes' => [
        'horizon*',
        'nova*',
        '_debugbar*',
        'sanctum*',
        'telescope*',
    ],

    // Model events to log
    'log_events' => [
        'created' => true,
        'updated' => true,
        'deleted' => true,
        'restored' => true,
    ],

    // Default properties to log for each activity
    'default_log_properties' => [
        'ip_address' => true,    // Log IP address
        'user_agent' => true,    // Log browser user agent
        'request_method' => true, // Log HTTP method
        'request_url' => true,    // Log full URL
    ],

    // Maximum size for the properties JSON column
    'properties_max_size' => 100000,

    // Enable automatic logging of authentication events
    'log_auth_events' => true,

    // Cleanup configuration for old records
    'cleanup' => [
        'enabled' => false,
        'keep_logs_for_days' => 365,
        'chunk_size' => 1000,
    ],

    // Date format for activity timestamps
    'date_format' => 'Y-m-d H:i:s',

    // Queue configuration for logging activities
    'queue' => [
        'enabled' => false,
        'connection' => env('QUEUE_CONNECTION', 'sync'),
        'queue' => 'activity-logs',
    ],

    // Default attributes to log for all models
    'log_attributes' => ['title', 'content', 'status'],

    // Default attributes to exclude from logging
    'log_except' => ['password', 'remember_token'],

    // Fields that should always be masked in the log
    'sensitive_fields' => ['password', 'credit_card', 'api_key'],
];

Usage

Automatic Model Activity Logging

Simply add the LogsActivity trait to any model you want to track:

use Loctracker\ActivityLogger\Traits\LogsActivity;

class Post extends Model
{
    use LogsActivity;

    // Optional: specify which attributes to log
    protected static $logAttributes = ['title', 'content', 'status'];

    // OR specify which attributes to exclude from logging
    protected static $logExcept = ['password', 'remember_token'];
}

Now all create/update/delete operations on this model will be automatically logged!

Configuring Attribute Logging

You can configure which attributes to log in three ways:

  1. In your model using properties:
use Loctracker\ActivityLogger\Traits\LogsActivity;

class Post extends Model
{
    use LogsActivity;

    // Option 1: Specify which attributes to log
    protected static $logAttributes = ['title', 'content', 'status'];

    // Option 2: Specify which attributes to exclude from logging
    protected static $logExcept = ['password', 'remember_token'];
}
  1. In the configuration file for all models:
// config/activity-logger.php

return [
    // Default attributes to log for all models
    'log_attributes' => ['title', 'content', 'status'],

    // Default attributes to exclude from logging
    'log_except' => ['password', 'remember_token'],

    // Fields that should always be masked in the log
    'sensitive_fields' => ['password', 'credit_card', 'api_key'],
];
  1. Let the package log all attributes (default behavior if no configuration is provided)

The priority order is:

  1. Model-specific configuration ($logAttributes or $logExcept in the model)
  2. Global configuration in config/activity-logger.php
  3. Log all attributes if no configuration is found

Sensitive fields defined in sensitive_fields will always be masked with '******' in the logs, regardless of how attributes are configured.

Retrieving Logs for a Model

// Get all activities for a specific post
$post = Post::find(1);
$activities = $post->activities;

// Each activity contains:
foreach ($activities as $activity) {
    echo $activity->action;        // 'created', 'updated', 'deleted'
    echo $activity->description;   // 'Post created 1', 'Post updated 1'
    echo $activity->causer;        // User who performed the action
    echo $activity->properties;    // Changed attributes and their values
}

Manual Logging

You can also log activities manually using the facade:

use Loctracker\ActivityLogger\Facades\ActivityLogger;

// Simple log
ActivityLogger::log(
    'custom-action',
    'Custom activity description',
    $model // optional
);

// Log with additional properties
ActivityLogger::withProperties(['key' => 'value'])
    ->log('custom-action', 'Custom activity with properties', $model);

// Log with specific causer
ActivityLogger::causedBy($user)
    ->log('custom-action', 'Activity by specific user', $model);

Cleanup Old Records

To clean up old activity logs, you can use the provided artisan command:

php artisan activitylog:clean

This command will delete records older than the configured retention period (keep_logs_for_days).

Customization

Custom Activity Model

You can use your own activity model by specifying it in the configuration:

'activity_model' => \App\Models\CustomActivityLog::class

Your custom model should extend the base ActivityLog model or implement the same interface.

Queue Configuration

To improve performance, you can enable queued logging:

'queue' => [
    'enabled' => true,
    'connection' => 'redis',
    'queue' => 'activity-logs',
]

This will process the activity logging in the background.

Testing

To run the tests for the Activity Logger Package, use PHPUnit:

vendor/bin/phpunit

License

This package is open-source and available under the MIT License. See the LICENSE file for more information.

Contributing

Contributions are welcome! If you would like to contribute:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate and follow the existing coding style.

motakabbir/activity-logger 适用场景与选型建议

motakabbir/activity-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-07