From ea3854a0eb3e378f4e29b84bcc5ca802a088a7b8 Mon Sep 17 00:00:00 2001 From: Przemek Sobstel Date: Wed, 2 Oct 2013 14:15:33 +0200 Subject: [PATCH 1/2] Add Loggly handler. --- src/Monolog/Handler/LogglyHandler.php | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Monolog/Handler/LogglyHandler.php diff --git a/src/Monolog/Handler/LogglyHandler.php b/src/Monolog/Handler/LogglyHandler.php new file mode 100644 index 00000000..dca713fe --- /dev/null +++ b/src/Monolog/Handler/LogglyHandler.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; +use Monolog\Formatter\JsonFormatter; + +/** + * Sends errors to Loggly. + */ +class LogglyHandler extends AbstractProcessingHandler +{ + const HOST = 'logs-01.loggly.com'; + + protected $token; + + protected $tag; + + public function __construct($token, $level = Logger::DEBUG, $bubble = true) + { + $this->token = $token; + $this->tag = $tag; + + parent::__construct($level, $bubble); + } + + public function setTag($tag) + { + $this->tag = $tag; + } + + protected function write(array $record) + { + $url = sprintf("http://%s/inputs/%s/", self::HOST, $this->token); + if ($this->tag) { + $url .= sprintf("tag/%s/", $this->tag); + } + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $record["formatted"]); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/plain']); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + curl_exec($ch); + curl_close($ch); + } + + protected function getDefaultFormatter() + { + return new JsonFormatter(); + } +} From 67f6a5a4f1d1051a852de1bb6ff6de4e6b9b3ba7 Mon Sep 17 00:00:00 2001 From: Przemek Sobstel Date: Wed, 2 Oct 2013 15:24:47 +0200 Subject: [PATCH 2/2] LogglyHandler: remove reundant tag assignment and PHP 5.4 specific syntax. --- src/Monolog/Handler/LogglyHandler.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Monolog/Handler/LogglyHandler.php b/src/Monolog/Handler/LogglyHandler.php index dca713fe..c6bfaace 100644 --- a/src/Monolog/Handler/LogglyHandler.php +++ b/src/Monolog/Handler/LogglyHandler.php @@ -28,7 +28,6 @@ class LogglyHandler extends AbstractProcessingHandler public function __construct($token, $level = Logger::DEBUG, $bubble = true) { $this->token = $token; - $this->tag = $tag; parent::__construct($level, $bubble); } @@ -50,7 +49,7 @@ class LogglyHandler extends AbstractProcessingHandler curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $record["formatted"]); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/plain']); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch);