PHP code example of vsavritsky / monolog-telegram

1. Go to this page and download the library: Download vsavritsky/monolog-telegram 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/ */

    

vsavritsky / monolog-telegram example snippets




$api_token = '123456789:teMbvbETojnSG93jDhnynvH8pT28H9TIB1h';    // Bot API token
$chat_id = 987654321;    // Target Chat ID
$level = Logger::ERROR;     // Log level
$bubble = true;     // Bubble up the stack or not
$use_curl = true;    // Use cURL or not? (default: use when available)
$timeout = 10;   // Timeout for API requests
$verify_peer = true;   // Verify SSL certificate or not? (development/debugging)

$logger = new Logger('My project');
$handler = new TelegramHandler($api_token, $chat_id, $level, $bubble, $use_curl, $timeout, $verify_peer);
$handler->setFormatter(new TelegramFormatter());    // Usage of this formatter is optional but recommended if you want better message layout
$logger->pushHandler($handler);

$logger->error('Error!');

$handler = new TelegramHandler('TOKEN', 123456789);
$handler->setFormatter(new TelegramFormatter());

// Combine all log entries into one and force batch processing
$handler = new BufferHandler($handler);

// Make sure that particular log stack wasn't sent before
$handler = new DeduplicationHandler($handler);

// Keep collecting logs until ERROR occurs, after that send collected logs to $handler
$handler = new FingersCrossedHandler($handler, new ErrorLevelActivationStrategy(Logger::ERROR));

$logger->pushHandler($handler);

$html = true;    // Choose whether to send the message in HTMl format
$format = "<b>%level_name%</b> (%channel%) [%date%]\n\n%message%\n\n%context%%extra%";   // Error (My project) [2018-05-01 15:55:15 UTC]
$date_format = 'Y-m-d H:i:s e';       // 2018-05-01 15:55:15 UTC, format must be supported by DateTime::format
$separator = '-';       // Seperation character for batch processing - when empty one empty line is used

$handler->setFormatter(new TelegramFormatter($html, $format, $date_format, $separator));