​​​​ php sentry | 苏生不惑的博客

php sentry

sentry 是一个

配置sentry

可以在sentry.io注册账号获取配置https://sentry.io/settings/sina-tl/sina/keys/ ,如 http://root:root@127.0.0.1:1234/2
当然也可以自己搭建服务器,参考 https://laravel-china.org/articles/4285/build-your-own-sentry-service

sentry扩展

1
2
3
4
5
6
7
8
9
composer require "sentry/sentry"
require_once APP_BASE_PATH.'/vendor/autoload.php';
$client = new \Raven_Client('http://root:root@127.0.0.1:1234/2');
$error_handler = new \Raven_ErrorHandler($client);
$error_handler->registerExceptionHandler();
$error_handler->registerErrorHandler();
$error_handler->registerShutdownFunction();
//直接写日志
$client->captureMessage('my log message');

monolog日志写入Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 composer require "monolog/monolog: 1.18.0"
composer require 'predis/predis'
use Monolog\Handler\RedisHandler;
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Processor\MemoryPeakUsageProcessor;
use Monolog\Processor\WebProcessor;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Handler\RavenHandler;
use Monolog\Logger;

class Monolog{
public $logger;

public function __construct($name)
{
$this->logger = new Logger($name);
//https://docs.sentry.io/clients/php/integrations/monolog/
$redis = new \Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'read_write_timeout' => 0,
]);
$handler = new RedisHandler($redis, "app:sentry", \Monolog\Logger::DEBUG);
$this->logger->pushHandler($handler);
$handler->setFormatter(new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, true));
$handler->pushProcessor(new MemoryPeakUsageProcessor(true));
$handler->pushProcessor(new IntrospectionProcessor());
$arr = [
'url' => 'REQUEST_URI',
'ip' => 'REMOTE_ADDR',
'http_method' => 'REQUEST_METHOD',
'query_string' => 'QUERY_STRING',
'cookies' => 'HTTP_COOKIE',
'host' => 'HTTP_HOST',
'referrer' => 'HTTP_REFERER',
'ua' => 'HTTP_USER_AGENT',
];

$handler->pushProcessor(new WebProcessor(null, $arr));
$this->logger->addinfo('applog',['info'=>'test']);
}

消费redis入sentry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use Raven_Client;
use Monolog\Handler\RavenHandler;
use Monolog\Formatter\LineFormatter;
while (true) {

$redis = new \Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'read_write_timeout' => 0,
]);
$data = $redis->lpop("app:sentry");

if (empty($data)) {
sleep(1);
continue;
}
$data = json_decode($data, true);
$raven_client= new Raven_Client('http://root:root@127.0.0.1:1234/3',[
'extra'=>$data['extra'],
'tags'=>[]
]);
$raven_hanlder=new RavenHandler($raven_client);

$data['extra'] = [];
$r=$raven_hanlder->handle($data);
$raven_client->__destruct();

查看错误日志

1
https://sentry.io/sina-tl/sina/issues/845243592/?query=is%3Aunresolved

php sentry

Docker部署Sentry监控Django应用并使用email+钉钉通知

php sentry usage

搭建私有的前端监控服务: sentry

sentry异常提醒

CentOS6 基于 Python 安装 Sentry