1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-23 17:46:09 +02:00

Merge remote-tracking branch 'JulienBreux/feature/logmatic'

This commit is contained in:
Jordi Boggiano
2016-07-02 15:05:16 +01:00
5 changed files with 275 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
/**
* Encodes message information into JSON in a format compatible with Logmatic.
*
* @author Julien Breux <julien.breux@gmail.com>
*/
class LogmaticFormatter extends JsonFormatter
{
/**
* @param string
*/
protected $hostname = '';
/**
* @param string
*/
protected $appname = '';
/**
* Set hostname
*
* @param string $hostname
*/
public function setHostname(string $hostname) {
$this->hostname = $hostname;
}
/**
* Set appname
*
* @param string $appname
*/
public function setAppname(string $appname) {
$this->appname = $appname;
}
/**
* Appends the 'hostname' and 'appname' parameter for indexing by Logmatic.
*
* @see http://doc.logmatic.io/docs/basics-to-send-data
* @see \Monolog\Formatter\JsonFormatter::format()
*/
public function format(array $record)
{
if (!empty($this->hostname)) {
$record['hostname'] = $this->hostname;
}
if (!empty($this->appname)) {
$record['appname'] = $this->appname;
}
return parent::format($record);
}
}

View File

@@ -0,0 +1,88 @@
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* 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\FormatterInterface;
use Monolog\Formatter\LogmaticFormatter;
/**
* @author Julien Breux <julien.breux@gmail.com>
*/
class LogmaticHandler extends SocketHandler
{
/**
* @var string
*/
protected $logToken;
/**
* @var string
*/
protected $hostname;
/**
* @var string
*/
protected $appname;
/**
* @param string $hostname Host name supplied by Logmatic.
* @param string $appname Application name supplied by Logmatic.
* @param string $token Log token supplied by Logmatic.
* @param bool $useSSL Whether or not SSL encryption should be used.
* @param int $level The minimum logging level to trigger this handler.
* @param bool $bubble Whether or not messages that are handled should bubble up the stack.
*
* @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing
*/
public function __construct($token, $hostname = '', $appname = '', $useSSL = true, $level = Logger::DEBUG, $bubble = true)
{
if ($useSSL && !extension_loaded('openssl')) {
throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogmaticHandler');
}
$endpoint = $useSSL ? 'ssl://api.logmatic.io:10515' : 'api.logmatic.io:10514';
$endpoint .= '/v1/';
parent::__construct($endpoint, $level, $bubble);
$this->logToken = $token;
$this->hostname = $hostname;
$this->appname = $appname;
}
/**
* {@inheritdoc}
*/
protected function generateDataStream($record): string
{
return $this->logToken . ' ' . $record['formatted'];
}
/**
* {@inheritdoc}
*/
protected function getDefaultFormatter(): FormatterInterface
{
$formatter = new LogmaticFormatter();
if (!empty($this->hostname)) {
$formatter->setHostname($this->hostname);
}
if (!empty($this->appname)) {
$formatter->setAppname($this->appname);
}
return $formatter;
}
}