Skip to content

Instantly share code, notes, and snippets.

@khobbits
Last active April 11, 2016 12:48
Show Gist options
  • Save khobbits/3e2e932831b21a35cb75e99bbc993e64 to your computer and use it in GitHub Desktop.
Save khobbits/3e2e932831b21a35cb75e99bbc993e64 to your computer and use it in GitHub Desktop.
Monolog with Logstash RabbitMQ PhpAmqpLib basic example
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\AmqpHandler;
use Monolog\Formatter\LogstashFormatter;
use PhpAmqpLib\Connection\AMQPStreamConnection;
// create a log channel
$log = new Logger('name');
//$log->pushHandler(new StreamHandler('your.log', Logger::WARNING));
//Replace log-endpoint with full hostname or IP address.
$connection = new AMQPStreamConnection("log-endpoint", "5672", 'guest', 'guest');
$channel = $connection->channel();
$rmqhandler = new AmqpHandler($channel, 'logs', Logger::INFO);
$rmqhandler->setFormatter(new LogstashFormatter("monolog"));
$log->pushHandler($rmqhandler);
// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment