mepsd/laravel-google-chat-logger

Send Laravel application logs to Google Chat with rich formatting, emoji support, and context data

v1.0.4 2025-02-16 17:45 UTC

This package is auto-updated.

Last update: 2025-06-16 18:40:01 UTC


README

Latest Version on Packagist Total Downloads License

Send your Laravel application logs directly to Google Chat with rich formatting, emoji support, threading, and automatic retries.

Features ๐ŸŒŸ

  • ๐ŸŽฏ Easy Integration: Works with Laravel's built-in logging system
  • ๐Ÿงต Message Threading: Group related logs into threads
  • ๐ŸŽจ Rich Formatting: Messages are beautifully formatted in Google Chat
  • ๐Ÿ”„ Automatic Retries: Built-in retry mechanism for failed messages
  • ๐ŸŽฏ Level-based Emojis: Different emojis for different log levels
  • ๐ŸŒ Environment Aware: Includes environment information in logs
  • โšก Performance: Configurable timeouts and retry settings
  • ๐Ÿšจ Exception Handling: Detailed exception formatting with stack traces

Requirements ๐Ÿ“‹

  • PHP 8.1 or higher
  • Laravel 10.x or 11.x
  • Google Chat space with webhook access

Installation ๐Ÿ’ฟ

  1. Install the package via composer:
composer require mepsd/laravel-google-chat-logger
  1. Add these variables to your .env file:
LOG_CHANNEL=google_chat
GOOGLE_CHAT_WEBHOOK_URL=your-webhook-url
  1. Add this to your config/logging.php channels array:
'channels' => [
    'google_chat' => [
        'driver' => 'custom',
        'via' => Mepsd\LaravelGoogleChatLogger\GoogleChatLogger::class,
        'url' => env('GOOGLE_CHAT_WEBHOOK_URL'),
        'level' => env('LOG_LEVEL', 'debug'),
        'retries' => 2,        // Number of retry attempts
        'timeout' => 5,        // Request timeout in seconds
    ],
],

Usage ๐Ÿ“

Basic Logging

// Simple info message
Log::info('User registered successfully', ['user_id' => 1]);

// Error with exception
try {
    // Some code
} catch (Exception $e) {
    Log::error('Process failed', [
        'exception' => $e,
        'user_id' => 1
    ]);
}

Message Threading

Group related logs into threads:

// Order Processing Thread
$threadKey = 'order-' . $orderId;

Log::info('Order received', [
    'thread_key' => $threadKey,
    'order_id' => $orderId,
    'amount' => 99.99
]);

Log::info('Processing payment', [
    'thread_key' => $threadKey,
    'payment_method' => 'credit_card'
]);

Log::info('Order completed', [
    'thread_key' => $threadKey,
    'status' => 'completed'
]);

// User Activity Thread
$userThread = 'user-' . $userId;

Log::info('User logged in', [
    'thread_key' => $userThread,
    'ip' => $request->ip()
]);

Log::warning('Failed login attempt', [
    'thread_key' => $userThread,
    'attempts' => 3
]);

Exception Handling

Exceptions are automatically formatted with details:

try {
    // Your code
} catch (Exception $e) {
    Log::error('Process failed', [
        'thread_key' => 'process-123',
        'exception' => $e,
        'additional_data' => $data
    ]);
}

Log Levels and Emojis

Each log level has its own emoji:

  • ๐Ÿšจ EMERGENCY - System is unusable
  • โš ๏ธ ALERT - Action must be taken immediately
  • ๐Ÿ”ฅ CRITICAL - Critical conditions
  • โŒ ERROR - Error conditions
  • โš ๏ธ WARNING - Warning conditions
  • ๐Ÿ“ NOTICE - Normal but significant conditions
  • โ„น๏ธ INFO - Informational messages
  • ๐Ÿ› DEBUG - Debug-level messages

Message Format

Messages in Google Chat will look like:

[local] โ„น๏ธ *INFO*

Your message here

Context: { "user_id": 123, "action": "login" }


## Configuration Options

Full configuration options:

```php
'google_chat' => [
    'driver' => 'custom',
    'via' => Mepsd\LaravelGoogleChatLogger\GoogleChatLogger::class,
    'url' => env('GOOGLE_CHAT_WEBHOOK_URL'),
    'level' => env('LOG_LEVEL', 'debug'),
    'retries' => 2,              // Number of retry attempts
    'timeout' => 5,              // Request timeout in seconds
],

Setting Up Google Chat Webhook ๐Ÿ”—

  1. Open your Google Chat space
  2. Click the space name to open the dropdown menu
  3. Select "Manage webhooks"
  4. Click "Add webhook"
  5. Name your webhook (e.g., "Laravel Logs")
  6. Copy the webhook URL
  7. Add the URL to your .env file

Best Practices ๐ŸŽฏ

  1. Use meaningful thread keys:
'user-{id}'        // For user activities
'order-{id}'       // For order processing
'deploy-{date}'    // For deployments
'job-{id}'         // For background jobs
  1. Group related logs:
$threadKey = 'payment-' . $paymentId;
Log::info('Starting payment', ['thread_key' => $threadKey]);
Log::info('Processing', ['thread_key' => $threadKey]);
Log::info('Completed', ['thread_key' => $threadKey]);
  1. Include context data:
Log::info('User action', [
    'thread_key' => 'user-123',
    'action' => 'profile_update',
    'changes' => ['name' => 'New Name'],
    'ip' => $request->ip()
]);

Testing ๐Ÿงช

composer test

Security ๐Ÿ”’

If you discover any security-related issues, please use the issue tracker.

Credits ๐Ÿ‘

License ๐Ÿ“„

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