Skip to content

Instantly share code, notes, and snippets.

@etobi
Created September 30, 2017 15:25
Show Gist options
  • Save etobi/18b2bc0fe7c02ce7f185962f5591a838 to your computer and use it in GitHub Desktop.
Save etobi/18b2bc0fe7c02ce7f185962f5591a838 to your computer and use it in GitHub Desktop.
Syslog in Slack posten
<?php
// ...
$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog'][] = \Foo\Bar\Hooks\Systemlog::class . '->customLog';
// ...
<?php
namespace Foo\Bar\Hooks;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class Systemlog
{
private $severityLabel = [
GeneralUtility::SYSLOG_SEVERITY_INFO => 'INFO',
GeneralUtility::SYSLOG_SEVERITY_NOTICE => 'NOTICE',
GeneralUtility::SYSLOG_SEVERITY_WARNING => 'WARNING',
GeneralUtility::SYSLOG_SEVERITY_ERROR => 'ERROR',
GeneralUtility::SYSLOG_SEVERITY_FATAL => 'FATAL',
];
protected $slackHook = 'https://hooks.slack.com/services/ABC/DEF'; // Adjust me // TODO move to config
protected $channel = '#foobar'; // Adjust me // TODO move to config
/**
* @param array $params 'msg', 'extKey', 'backTrace', 'severity'
* @param $ref
*/
public function customLog($params, $ref)
{
if (preg_match('/^Uncaught TYPO3 Exception:/', $params['msg'])
&& !preg_match('/#1417988921:/', $params['msg']) // ignore CSFR protection errors
) {
$this->sendSlackMessage($params['extKey'], $params['msg'], $params['severity']);
}
}
/**
* @param string $msg
*/
protected function sendSlackMessage($extKey, $msg, $severity)
{
$slackClient = new \Maknz\Slack\Client(
$this->slackHook,
[
'username' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
'channel' => $this->channel,
'link_names' => true,
'icon' => ':unicorn_face:',
],
new \GuzzleHttp\Client(['verify' => false])
);
/** @noinspection PhpUndefinedMethodInspection */
$slackClient->send(
$this->severityLabel[$severity] . ': ' .
str_replace('|', "\n", $msg) .
($extKey ? ' (EXT: ' . $extKey . ')' : '' ) .
// "\n @channel "
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment