giacomofurlan / php-graylog-gelf
A PHP PSR-3 compliant logger to send data to graylog2 (GELF format)
Installs: 7 879
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/giacomofurlan/php-graylog-gelf
Requires (Dev)
- php-mock/php-mock-phpunit: ^2.1
- phpunit/phpunit: ^7.3
This package is auto-updated.
Last update: 2025-11-09 17:35:36 UTC
README
This library aims to help the developer with both a PSR-3 compliant and a custom (extended) logger to send data to Graylog 2 via the Graylog Extended Log Format.
Class information
GiacomoFurlan\Graylog\GELFLogger
It implements the Psr\Log\LoggerInterface interface, so use it as a standard logger.
It can set the short message, the context and the log level.
GiacomoFurlan\Graylog\ExtendedGELFLogger
It extends the previous class, but add gelf* functions to send more complex information.
It also allows the logs to be enqueued, rather than being sent immediately.
gelfLoggelfAlertgelfCriticalgelfDebuggelfEmergencygelfErrorgelfInfogelfNoticegelfWarningflush
These function accept a GELF object and the possibility to flush the messages instantly (default true).
The flush function flushes eventual messages previously enqueued.
GiacomoFurlan\Graylog\GELF
A data-transfer object used by ExtendedGELFLogger. It allows to set the short and full messages, to overwrite
the host information and to add variables to the context.
GiacomoFurlan\Graylog\UDPWriter
UDP implementation of the WriterInterface used to instantiate the loggers.
If there are enqueued messages (writing with flush set to false, changeable using the extended logger),
it will try to run flush when destroyed.
GiacomoFurlan\Graylog\TCPWriter
UDP implementation of the WriterInterface used to instantiate the loggers.
If there are enqueued messages (writing with flush set to false, changeable using the extended logger),
it will try to run flush when destroyed.
GiacomoFurlan\Graylog\GELFException
It can be thrown trying to send the information.
- Code
GELFException::CODE_MISSING_HOST: the host is's not set or empty - Code
GELFException::CODE_CANT_SEND_MESSAGE: an error occurred trying to send the packet
More information may be gathered reading the exception's message.
Usage example
use GiacomoFurlan\Graylog\ExtendedGELFLogger; use GiacomoFurlan\Graylog\GELF; use GiacomoFurlan\Graylog\GELFException; use GiacomoFurlan\Graylog\GELFLogger; use GiacomoFurlan\Graylog\UDPWriter; use Psr\Log\LogLevel; // ... $address = 'ip.or.dns.of.graylog.server'; $port = 12201; // or whatever port is configured in Graylog 2 $hostId = 'MyHost'; // host identifier // Instantiate the writer $writer = new UDPWriter($address, $port); // Instantiate the logger (use one or the other... or both) $simpleLogger = new GELFLogger($writer, $hostId); $extendedLogger = new ExtendedGELFLogger($writer, $hostId); // ... // GELFLogger usage example try { // ... } catch (\Throwable $exception) { $simpleLogger->log(LogLevel::ERROR, $exception->getMessage(), ['code' => $exception->getCode()]); // or $simpleLogger->error($exception->getMessage(), ['code' => $exception->getCode()]); } // ExtendedGELFLogger usage example try { // ... } catch (\Throwable $exception) { $message = new GELF($exception->getMessage(), ['code' => $exception->getCode()]); $message->setHost('Overwritten host'); $message->setFullMessage($exception->getTraceAsString()); $message->setContextEntryset('fileName', $exception->getFile()); $message->setContextEntryset('fileLine', $exception->getLine()); $extendedLogger->gelfError($message); }