PHP code example of theriddleofenigma / laravel-google-chat-log

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

    

theriddleofenigma / laravel-google-chat-log example snippets


use Illuminate\Support\Facades\Log;

Log::channel('google-chat')->error('Something went wrong');

'google-chat' => [
    'driver' => 'monolog',
    'handler' => \Enigma\GoogleChatHandler::class,
    'url' => env('LOG_GOOGLE_CHAT_WEBHOOK_URL'),
    'level' => env('LOG_GOOGLE_CHAT_LEVEL', env('LOG_LEVEL', 'debug')),
    'notify_users' => [
        'default' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT'),
        'emergency' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_EMERGENCY'),
        'alert' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_ALERT'),
        'critical' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_CRITICAL'),
        'error' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_ERROR'),
        'warning' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_WARNING'),
        'notice' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_NOTICE'),
        'info' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_INFO'),
        'debug' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEBUG'),
    ],
],

'google-chat-ops' => [
    'driver' => 'custom',
    'via' => \Enigma\GoogleChatLogger::class,
    'name' => 'google-chat-ops',
    'url' => env('LOG_GOOGLE_CHAT_OPS_WEBHOOK_URL'),
    'level' => 'critical',
    'notify_users' => ['default' => 'all'],
],

'google-chat-ops' => [
    'driver' => 'monolog',
    'handler' => \Enigma\GoogleChatHandler::class,
    'with' => ['channel' => 'google-chat-ops'],
    'url' => env('LOG_GOOGLE_CHAT_OPS_WEBHOOK_URL'),
    'level' => 'critical',
    'notify_users' => ['default' => 'all'],
],

Log::build([
    'driver' => 'monolog',
    'handler' => \Enigma\GoogleChatHandler::class,
    'with' => ['config' => ['url' => 'https://chat.googleapis.com/v1/spaces/...']],
])->error('Something went wrong');

use Enigma\GoogleChatHandler;
use Monolog\LogRecord;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        GoogleChatHandler::$additionalLogs = function (LogRecord $record) {
            return [
                'tenant' => request()->user()?->tenant->name,
                'request' => request()->fullUrl(),
            ];
        };
    }
}

// config/logging.php
'google-chat' => [
    // ...
    'additional_logs' => \App\Logging\ChatExtras::class,
],

// app/Logging/ChatExtras.php
class ChatExtras
{
    public function __invoke(LogRecord $record): array
    {
        return ['tenant' => request()->user()?->tenant->name];
    }
}

GoogleChatHandler::additionalLogsFor('google-chat-ops', fn (LogRecord $record) => [
    'queue' => $record->context['queue'] ?? null,
]);
dotenv
LOG_GOOGLE_CHAT_WEBHOOK_URL=https://chat.googleapis.com/v1/spaces/XXXX/messages?key=...&token=...
shell
php artisan vendor:publish --tag=google-chat-log-config
shell
php artisan google-chat-log:test
php artisan google-chat-log:test --channel=google-chat-ops --level=critical