PHP code example of motakabbir / activity-logger

1. Go to this page and download the library: Download motakabbir/activity-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

motakabbir / activity-logger example snippets


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'],
];

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'];
}

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'];
}

// 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'],
];

// 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
}

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);

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

'queue' => [
    'enabled' => true,
    'connection' => 'redis',
    'queue' => 'activity-logs',
]
bash
php artisan vendor:publish --provider="Loctracker\ActivityLogger\ActivityLoggerServiceProvider"
bash
php artisan migrate
bash
php artisan activitylog:clean