Skip to content

Commit

Permalink
Added additional examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bzikarsky committed Jan 23, 2014
1 parent dea5bee commit c5d4c52
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
38 changes: 38 additions & 0 deletions examples/advanced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the php-gelf package.
*
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__ . '/../vendor/autoload.php';

// We need a transport - UDP via port 12201 is standard.
$transport = new Gelf\Transport\UdpTransport("127.0.0.1", 12201, Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN);

// While the UDP transport is itself a publisher, we wrap it in a real Publisher for convenience
// A publisher allows for message validation before transmission, and it calso supports to send messages
// to multiple backends at once
$publisher = new Gelf\Publisher();
$publisher->addTransport($transport);

// Now we can create custom messages and publish them
$message = new Gelf\Message();
$message->setShortMessage("Foobar!")
->setLevel(\Psr\Log\LogLevel::ALERT)
->setFullMessage("There was a foo in bar")
->setFacility("example-facility")
;
$publisher->publish($message);


// The implementation of PSR-3 is encapsulated in the Logger-class.
// It provides high-level logging methods, such as alert(), info(), etc.
$logger = new Gelf\Logger($publisher, "example-facility");

// Now we can log...
$logger->alert("Foobaz!");
19 changes: 19 additions & 0 deletions examples/simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the php-gelf package.
*
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__ . '/../vendor/autoload.php';

// When creating a logger without any options, it logs automatically to localhost:12201 via UDP
// For a move advanced configuration, check out the advanced.php example
$logger = new Gelf\Logger();

// Log!
$logger->alert("Foobaz!");
4 changes: 2 additions & 2 deletions src/Gelf/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Logger extends AbstractLogger implements LoggerInterface
protected $facility;

/**
* @var Publisher
* @var PublisherInterface
*/
protected $publisher;

Expand All @@ -39,7 +39,7 @@ class Logger extends AbstractLogger implements LoggerInterface
* @param Publisher $publisher
* @param string $facility
*/
public function __construct(Publisher $publisher = null, $facility = null)
public function __construct(PublisherInterface $publisher = null, $facility = null)
{
// if no publisher is provided build a "default" publisher
// which is logging via Gelf over UDP to localhost on the default port
Expand Down
11 changes: 10 additions & 1 deletion src/Gelf/Transport/UdpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
use Gelf\Encoder\EncoderInterface;
use Gelf\MessageInterface as Message;
use Gelf\Encoder\CompressedJsonEncoder as DefaultEncoder;
use Gelf\MessageInterface;
use Gelf\PublisherInterface;
use RuntimeException;

/**
* UdpTransport allows the transfer of GELF-messages to an compatible GELF-UDP-backend
* as described in https://github.com/Graylog2/graylog2-docs/wiki/GELF
*
* It can also act as a direct publisher
*
* @author Benjamin Zikarsky <[email protected]>
*/
class UdpTransport implements TransportInterface
class UdpTransport implements TransportInterface, PublisherInterface
{
const CHUNK_GELF_ID = "\x1e\x0f";
const CHUNK_MAX_COUNT = 256; // sequence-size is stored in a CHAR
Expand Down Expand Up @@ -116,6 +120,11 @@ public function send(Message $message)
return 1;
}

public function publish(Message $message)
{
$this->send($message);
}

/**
* Sends given string in multiple chunks
*
Expand Down

0 comments on commit c5d4c52

Please sign in to comment.