How to capture PHP errors, functions or methods using Monolog?

Asked

Viewed 47 times

1

Good afternoon, I’m trying to create error checker, for whenever you have any particular PHP error on client side send register a Log.

My problem is this, how can I capture PHP errors to upload to this log? I can already send the message and even send information from the server however, due to the lack of information on the internet, I could not understand how I can call this file in my pages to always run and take these parts that gave errors and pass to the message I want to send in PHP.

<?php 
use Monolog\Handler\BrowserConsoleHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SendGridHandler;
use Monolog\Handler\TelegramBotHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use Monolog\ErrorHandler;

require __DIR__."/vendor/autoload.php";

$logger = new Logger("CONNECT");
$logger->pushHandler(new BrowserConsoleHandler(Logger::DEBUG));
$logger->pushHandler(new StreamHandler(__DIR__."/log.txt",Logger::WARNING));
$logger->pushHandler(new StreamHandler("php://output", Logger::ERROR));
$logger->pushProcessor(new IntrospectionProcessor(Logger::DEBUG));
$logger->pushProcessor(function($record){
    $record["extra"]["SCRIPT_NAME"] = $_SERVER["SCRIPT_NAME"];
    });
$logger->pushProcessor(new IntrospectionProcessor(Logger::DEBUG));

//TELEGRAM
$tele_key = "meuapiid";
$tele_channel = "meapikey";
$tele_handler = new TelegramBotHandler($tele_key, $tele_channel,Logger::EMERGENCY);
$tele_handler->setFormatter(new LineFormatter("%level_name%\n %channel% \n [%datetime%]  \n%context% \n %message% \n %extra%"));
$logger->pushHandler($tele_handler);

$logger->debug("Erro encontrado!");
$logger->info("Erro encontrado!");
$logger->notice("Erro encontrado!");
$logger->warning("Erro encontrado!");
$logger->error("Erro encontrado!");
$logger->critical("Erro encontrado!");
$logger->alert("Erro encontrado!");
$logger->emergency("Erro encontrado!");
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.