M
M
mrWan2018-08-21 02:59:00
Laravel
mrWan, 2018-08-21 02:59:00

How to throw out logs from laravel, for example, to mail, telegrams?

I know how to send a letter to the mail or a message in a telegram, I have difficulties only in Laravel, when errors occur, write these errors into a variable and run your own method, which will send information about errors somewhere without affecting the built-in logging out of the box. I opened the logging documentation, tried a lot of things, but nothing helped.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2018-08-21
@xpert13

app/Exceptions/Handler.php

class Handler extends ExceptionHandler
{
  /**
   * Report or log an exception.
   *
   * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
   *
   * @param  \Exception  $exception
   * @return void
   */
  public function report(Exception $exception) {
    if ($this->shouldReport($exception)) {
        // Send report
    }
  }

A
Alexander Talalaev, 2018-08-21
@neuotq

In fact, everything is simple, if you read the documentation , you can find out about a cool class App\Exceptions\Handlerthat handles exceptions, you need its method report, which you can override and write your own logic.
An example of sending an email is even easy to google (the person even made his own package https://github.com/squareboat/sneaker ).
In short, here, and go ahead to create magic:
5b7bb11abd163261551026.png

E
Evgeny Bukharev, 2021-07-14
@evgenybuckharev

in config/logging.php

'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single', 'telegram'],
            'ignore_exceptions' => false,
        ],

        'telegram' => [
            'driver' => 'monolog',
            'handler' => Monolog\Handler\TelegramBotHandler::class,
            'with' => [
                'apiKey' => env('TELEGRAM_BOT_TOKEN'),
                'channel' => env('TELEGRAM_BOT_CHAT_ID'),
            ],
        ],
  ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question