Created
December 18, 2013 08:57
-
-
Save jqlblue/8019312 to your computer and use it in GitHub Desktop.
sentry php client for collection php5.5 error log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Client | |
{ | |
private static $_logFile; | |
private static $_parseOffset; | |
private static $_dsn; | |
private static $_logtailBin; | |
private static $_maxItem; | |
public static function run($options) | |
{ | |
self::_initConfig($options); | |
self::_sendMessage(); | |
} | |
private static function _getInstance() | |
{ | |
$client = new Raven_Client(self::$_dsn, array( | |
'tags' => array( | |
'php_version' => phpversion(), | |
), | |
'trace' => false, | |
)); | |
return $client; | |
} | |
private static function _initConfig($options) | |
{ | |
if (empty($options['logfile'])) { | |
self::$_logFile = '/opt/php-5.5.4/log/php_errors.log'; | |
} else { | |
self::$_logFile = $options['logfile']; | |
} | |
self::$_parseOffset = dirname(__FILE__) . '/log/' . $options['project'] . '.offset'; | |
self::$_dsn = 'udp://public:secret@10.16.15.1/3'; | |
self::$_logtailBin = '/usr/sbin/logtail2'; | |
self::$_maxItem = 100; | |
} | |
private static function _parseMessage(& $messageString) | |
{ | |
foreach (explode("\n", trim($messageString)) as $message) { | |
if (strpos($message, 'PRC] PHP')) { | |
yield $message; | |
} | |
} | |
} | |
private static function _getSendInfo($message) | |
{ | |
$rows = explode(" ", $message); | |
$info['option']['timestamp'] = substr(array_shift($rows), 1) . ' ' . array_shift($rows); | |
$info['option']['timestamp'] = date('Y-m-d\TH:i:s\Z', strtotime($info['option']['timestamp'])); | |
$info['option']['level'] = strtolower(substr($rows[2], 0, -1)); | |
$info['message'] = implode(" ", array_slice($rows, 4)); | |
return $info; | |
} | |
private static function _sendMessage() | |
{ | |
$client = self::_getInstance(); | |
$command = self::$_logtailBin . ' -f ' . self::$_logFile . ' -o ' . self::$_parseOffset . ' | tail -n ' . self::$_maxItem; | |
$messageString = shell_exec($command); | |
foreach (self::_parseMessage($messageString) as $message) { | |
$sendInfo = self::_getSendInfo($message); | |
$client->captureMessage($sendInfo['message'], array(), $sendInfo['option']); | |
} | |
} | |
} | |
require(dirname(__FILE__) . '/lib/Raven/Autoloader.php'); | |
Raven_Autoloader::register(); | |
$longopts = array( | |
'project:', | |
'logfile:', | |
); | |
$options = getopt('', $longopts); | |
if (empty($options['project'])) { | |
die('project is required'); | |
} | |
Client::run($options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment