PHP code example of teaminfinitydev / laravel-activity-log-discord

1. Go to this page and download the library: Download teaminfinitydev/laravel-activity-log-discord 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/ */

    

teaminfinitydev / laravel-activity-log-discord example snippets


$this->client = new Client([
    'timeout' => 30,
    'connect_timeout' => 10,
    'verify' => true, // SSL verification enabled
]);

$this->client = new Client([
    'timeout' => 30,
    'connect_timeout' => 10,
    'verify' => false, // SSL verification disabled
]);

use teaminfinitydev\ActivityLogDiscord\Facades\ActivityLogger;

// Simple event logging
ActivityLogger::log('user.action', 'User performed a custom action');

// Detailed logging with subject and causer
ActivityLogger::log(
    'order.completed',
    'Order #1234 has been completed',
    $order,        // Subject (the order)
    $user,         // Causer (who performed the action)
    ['total' => 99.99, 'items' => 3] // Additional properties
);

// User activity logging
ActivityLogger::logUserLogin($user);
ActivityLogger::logUserLogout($user);

// Model activity logging
ActivityLogger::logModelCreated($post, $user);
ActivityLogger::logModelUpdated($post, $changes, $user);
ActivityLogger::logModelDeleted($post, $user);

// System events
ActivityLogger::logWebAppBootup();

// Test webhook
$success = ActivityLogger::testWebhook();



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use teaminfinitydev\ActivityLogDiscord\Traits\LogsActivity;

class Post extends Model
{
    use LogsActivity;
    
    // Specify which events to log
    protected $logActivity = ['created', 'updated', 'deleted', 'restored'];
    
    // Optional: Customize the display name
    public function getDisplayName(): string
    {
        return $this->title;
    }
    
    // Optional: Custom logging conditions
    public function shouldLogActivity(string $event): bool
    {
        // Don't log updates if only timestamps changed
        if ($event === 'updated') {
            return count($this->getDirty()) > 2; // more than created_at and updated_at
        }
        
        return true;
    }
    
    // Optional: Additional properties for activity log
    public function getActivityLogProperties(string $event): array
    {
        return [
            'category' => $this->category,
            'status' => $this->status,
        ];
    }
}



namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use teaminfinitydev\ActivityLogDiscord\Facades\ActivityLogger;

class LoginController extends Controller
{
    protected function authenticated(Request $request, $user)
    {
        ActivityLogger::logUserLogin($user);
    }
    
    public function logout(Request $request)
    {
        if (auth()->check()) {
            ActivityLogger::logUserLogout(auth()->user());
        }
        
        $this->guard()->logout();
        $request->session()->invalidate();
        
        return redirect('/');
    }
}

public function __construct(?string $webhookUrl, string $botName = 'Activity Logger', ?string $avatarUrl = null)
{
    $this->client = new Client([
        'timeout' => 30,
        'connect_timeout' => 10,
        'verify' => false, // Disable SSL verification for testing
    ]);
    // Rest of the constructor...
}

try {
    // In your DiscordWebhookService.php
    Log::debug('Attempting to connect to Discord webhook', [
        'webhook_url' => $this->maskWebhookUrl($this->webhookUrl),
    ]);
    
    $response = $this->client->post($this->webhookUrl, [
        // your existing options
        'debug' => true, // Add this to see detailed request/response info
    ]);
} catch (RequestException $e) {
    Log::error('Discord webhook error details', [
        'request_exception_details' => method_exists($e, 'getHandlerContext') ? $e->getHandlerContext() : 'Not available',
    ]);
}

'events' => [
    'user.login' => [
        'enabled' => true,
        'color' => 0x00ff00, // Green
        'icon' => '🔐',
    ],
    'order.failed' => [
        'enabled' => true,
        'color' => 0xff0000, // Red
        'icon' => '❌',
    ],
],

// config/activity-log-discord.php
'queue_notifications' => true,
'queue_connection' => 'redis', // or your preferred connection
'queue_name' => 'discord-notifications',
bash
php artisan vendor:publish --provider="teaminfinitydev\ActivityLogDiscord\ActivityLogDiscordServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="teaminfinitydev\ActivityLogDiscord\ActivityLogDiscordServiceProvider" --tag="migrations"
php artisan migrate

🚀 System Bootup
Web application started successfully

Details:
Environment: production
PHP Version: 8.2.0
Laravel Version: 10.0.0
Memory Usage: 32.5 MB
Server Time: 2024-01-15 14:30:22
bash
php artisan queue:work