diff --git a/examples/advanced.php b/examples/advanced.php new file mode 100644 index 0000000..2f0f0e2 --- /dev/null +++ b/examples/advanced.php @@ -0,0 +1,38 @@ + + * + * 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!"); diff --git a/examples/simple.php b/examples/simple.php new file mode 100644 index 0000000..151c4cd --- /dev/null +++ b/examples/simple.php @@ -0,0 +1,19 @@ + + * + * 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!"); diff --git a/src/Gelf/Logger.php b/src/Gelf/Logger.php index b4d18a3..d630ad9 100644 --- a/src/Gelf/Logger.php +++ b/src/Gelf/Logger.php @@ -29,7 +29,7 @@ class Logger extends AbstractLogger implements LoggerInterface protected $facility; /** - * @var Publisher + * @var PublisherInterface */ protected $publisher; @@ -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 diff --git a/src/Gelf/Transport/UdpTransport.php b/src/Gelf/Transport/UdpTransport.php index e86e10b..66c78ef 100644 --- a/src/Gelf/Transport/UdpTransport.php +++ b/src/Gelf/Transport/UdpTransport.php @@ -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 */ -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 @@ -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 *