Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Efficient Logging with Silex, Monolog & Telegram

Efficient Logging with Silex, Monolog & Telegram

How to implement a Telegram Handler in order to deliver critical log messages over Telegram Messaging Application using Monolog and Silex.

Ricardo Coelho

July 08, 2015
Tweet

More Decks by Ricardo Coelho

Other Decks in Programming

Transcript

  1. Silex <?php chdir(dirname(__DIR__)); require ‘vendor/autoload.php’; use Silex\Application; $app = new

    Application(); $app->get(‘/’, function() { return new Response(‘Hello World!’, 200); }); $app->run();
  2. Who Monologs • Symfony2 • Silex • Laravel 4 &5

    • Lumen • PPI • XOOPS 2.6 • Aura • Proton • Plugins (CodeIgniter, Nette, Slim, …)
  3. Monolog <?php use Monolog\Logger; use Monolog\Handler\StreamHandler; $log = new Logger('channel');

    $log->pushHandler(new StreamHandler( 'path/to/your.log', Logger::WARNING )); $log->addWarning('Foo'); $log->addError('Bar');
  4. Silex MonologProvider <?php chdir(dirname(__DIR__)); require 'vendor/autoload.php'; use Silex\Application; $app =

    new Application(); $app->get('/', function() { return new Response(‘Hello World!’, 200); }); $app->run(); $app->register(new MonologProvider());
  5. MonologProvider class MonologProvider implements ServiceProviderInterface { public function boot(Application $app)

    { ErrorHandler::Register( $app['monolog'], $error_mapping, LogLevel::ERROR, LogLevel::ERROR ); if (isset($app['monolog.listener'])) { $app['dispatcher']->addSubscriber( $app['monolog.listener'] ); } } }
  6. MonologProvider public function register(Application $app) { $app['monolog'] = $app->share(function($app) {

    $app['monolog.listener'] = $app->share(function () use ($app) { return new LogListener($app['logger']); }); $monolog = new Logger('channel'); $monolog->pushHandler(new ErrorLogHandler()); return $monolog; }); $app['logger'] = function () use ($app) { return $app['monolog']; }; } }
  7. Telegram Handler class TelegramHandler extends MailHandler { (…) public function

    __construct( Client $telegram, $contact, $level = Logger::ERROR, $bubble = true ) { parent::__construct($level, $bubble); $this->telegram = $telegram; $this->contact = $contact; } }
  8. Telegram Handler class TelegramHandler extends MailHandler { protected $telegram; protected

    $contact; public function __construct(…) {…} protected function send($content, $records) { $this->telegram->msg( $this->contact, $content ); } }
  9. MonologProvider class MonologProvider implements ServiceProviderInterface { public function register(Application $app)

    { (…) if (file_exists(\TG_SOCKET)) { $telegram = new Client('unix://' . \TG_SOCKET); $monolog->pushHandler(new TelegramHandler( $telegram, \TG_CONTACT )); } (…) } define('TG_CONTACT', 'chat#365532'); define('TG_SOCKET', '/tmp/tg.sck');
  10. getMe { "ok": true, "result": { "id": 195108421, "first_name": "PHP

    Conference", "username": "PHPConferenceBot" } }