GithubHelp home page GithubHelp logo

theriddleofenigma / laravel-google-chat-log Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 5.0 42 KB

Brings up the option for sending the logs to google chat [Google Workplace formerly called GSuite].

Home Page: https://theriddleofenigma.github.io/laravel-google-chat-log/

License: MIT License

PHP 100.00%
laravel lumen gsuite google-chat laravel-logs logs error-logging

laravel-google-chat-log's Introduction

♥ Made with <love/> And I love <code/>

Laravel Google Chat Log

Brings up the option for sending the logs to google chat [Google Workspace formerly called GSuite] from Laravel/Lumen.

Installation

Composer install

composer require theriddleofenigma/laravel-google-chat-log

For laravel 9.x or lower, please use v1.x

composer require theriddleofenigma/laravel-google-chat-log:^1.3

Add the following code to the channels array in config/logging.php in your laravel/lumen application.

'google-chat' => [
    'driver' => 'monolog',
    'url' => env('LOG_GOOGLE_CHAT_WEBHOOK_URL'),
    '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'),
    ],
    'level' => env('LOG_LEVEL', 'debug'),
    'handler' => \Enigma\GoogleChatHandler::class,
],

You can provide the eight logging levels defined in the RFC 5424 specification: emergency, alert, critical, error, warning, notice, info, and debug

Note*: Make sure to set the LOG_GOOGLE_CHAT_WEBHOOK_URL env variable. And all other LOG_GOOGLE_CHAT_NOTIFY_USER_ID are optional. Here, you can set multiple google chat webhook url as comma separated value for the LOG_GOOGLE_CHAT_WEBHOOK_URL env variable.

Note*: For lumen, make sure the $app->withFacades(); is uncommented in the bootstrap/app.php.

Now, you can notify a specific user with @mention in the error log by setting the corresponding USER_ID to the LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT env variable. User Ids mapped under LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT will be notified for all log levels.

For getting the USER_ID, right-click the user-icon of the person whom you want to notify in the Google chat from your browser window and select inspect. Under the div element find the attribute data_member_id, then the USER_ID can be found as data-member-id="user/human/{USER_ID}>".

In order to notify all the users like @all, Set LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT=all. Also, you can set multiple USER_IDs as comma separated value. In order to notify different users for different log levels, you can set the corresponding env keys mentioned to configure in the logging.php file.

Now, you can add custom additional logs to the Google chat message by passing a closure function to the GoogleChatHandler::$additionalLogs property.

use Enigma\GoogleChatHandler;
use Illuminate\Http\Request;

class AppServiceProvider {
    public function register() {}
    public function boot() {
        GoogleChatHandler::$additionalLogs = function () {
            return [
                'tenant' => request()->user()?->tenant->name,
                'request' => json_encode(request()->toArray()),
            ];
        };
    }
}

License

Copyright © Kumaravel

Laravel Google Chat Log is open-sourced software licensed under the MIT license.

laravel-google-chat-log's People

Contributors

theriddleofenigma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-google-chat-log's Issues

FatalError while log

Error Description

I have installed the package using composer and then added config in logging and the URL in env. php and then while any error throws then the error must be logged in the chat but here I found an exception like the one below.

Declaration of Enigma\GoogleChatHandler::write($record) must be compatible with Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): void

Note: This is happened in the lumen.

Symfony\Component\ErrorHandler\Error
FatalError
Show exception properties
Symfony\Component\ErrorHandler\Error\FatalError {#111 ▼
-error: array:4 [▼
"type" => 64
"message" => "Declaration of Enigma\GoogleChatHandler::write($record) must be compatible with Monolog\Handler\AbstractProcessingHandler::write(Monolog\LogRecord $record): voi ▶"
"file" => "/var/www/vendor/theriddleofenigma/laravel-google-chat-log/src/GoogleChatHandler.php"
"line" => 19
]
}

Can a space be notified instead of a person?

Hello,

is it possible to notify a space instead of a human? For example - we have a few Dev spaces our IT/Dev team uses for automated server stuff.

The ID (when inspecting our space icons) looks like data-group-id="space/XXXXXXXX"

Thanks in advance!

Missing ServiceProvider?

I had to create a ServiceProvider

<?php

namespace App\Providers;

use Enigma\GoogleChatHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class GoogleChatServiceProvider extends ServiceProvider
{

	/**
	 * Register Papertrail Service Provider
	 *
	 * @return void
	 */
	public function register()
	{
		$monolog = Log::getLogger();
		$gch = new GoogleChatHandler();
		$gch->setLevel(config('logging.channels.google-chat.level'));
		$monolog->pushHandler($gch);
	}
} 

And add it to config\app.php to get it to work (adding App\Providers\GoogleChatServiceProvider::class, in the 'providers' section).

Did I miss something in your setup description from the Readme? Perhaps you could add this to your setup description?
I'm using Laravel 9.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.