Skip to content

Instantly share code, notes, and snippets.

@stmllr
Created May 15, 2013 14:22
Show Gist options
  • Save stmllr/5584331 to your computer and use it in GitHub Desktop.
Save stmllr/5584331 to your computer and use it in GitHub Desktop.
Experimental implementation of a greylog2 LogWriter for TYPO3 CMS >= 6.x
<?php
namespace T3node\Graylog2Writer\Log\Writer;
/***************************************************************
* Copyright notice
*
* (c) 2013 Steffen Müller <typo3@t3node.com>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
require_once PATH_typo3conf . 'ext/graylog2_writer/Classes/Contrib/Graylog2/GELFMessage.php';
require_once PATH_typo3conf . 'ext/graylog2_writer/Classes/Contrib/Graylog2/GELFMessagePublisher.php';
/**
* LogWriter for the TYPO3 Logging API.
* Sends Log records as GELF messages to GrayLog2 Server
*
*/
class Graylog2Writer extends \TYPO3\CMS\Core\Log\Writer\AbstractWriter {
/** @var string */
const GRAYLOG2_GELF_VERSION = '1.0';
/** @var string */
protected $host = '';
/** @var integer */
protected $port = NULL;
/** @var integer */
protected $chunkSize = NULL;
/**
* Renders the E-Mail
*
* @param \TYPO3\CMS\Core\Log\LogRecord $record
* @return Graylog2Writer
*/
public function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) {
if (empty($this->host)) {
return $this;
}
/** @var \GELFMessage $message */
$message = new \GELFMessage();
$message->setVersion(self::GRAYLOG2_GELF_VERSION);
$message->setLevel($record->getLevel());
$message->setHost(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST'));
$message->setShortMessage($record->getMessage());
$message->setFile($record->getComponent());
// @TODO enhance mapping to better fit GELF message properties
foreach ($record->getData() as $key => $value) {
$message->setAdditional($key, $value);
}
/** @var \GELFMessagePublisher $publisher */
$publisher = new \GELFMessagePublisher($this->host, $this->port, $this->chunkSize);
$publisher->publish($message);
return $this;
}
/**
* Sets host
*
* @param string $host
* @return void
*/
public function setHost($host) {
$this->host = $host;
}
/**
* Sets port
*
* @param integer $port
* @return void
*/
public function setPort($port) {
$this->port = $port;
}
/**
* Sets chunkSize
*
* @param integer $chunkSize
* @return void
*/
public function setChunkSize($chunkSize) {
$this->chunkSize = $chunkSize;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment