目标:
实现laravel日志推送到sentry,进行日志追踪,能够及时发现服务的异常情况。
docker 安装
#安装依赖
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
#配置仓库
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#安装最新版本docker
$ sudo yum install docker-ce docker-ce-cli containerd.io
安装docker-compose
#下载并保存
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#增加执行权限
$ sudo chmod +x /usr/local/bin/docker-compose
#增加软连接
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ docker-compose --version
docker-compose version 1.24.1, build 1110ad01
sentry 安装 (docker)
#获取仓库
$ git clone https://github.com/getsentry/onpremise.git
$ cd onpremise
#安装,期间会需要输入用户名与密码,用于后续登陆
$ ./install.sh
# netstat 查看sentry是否成功启动 (sentry默认配置9000端口)
$ netstat -ltpn
tcp6 0 0 :::9000 :::* LISTEN 29884/docker-proxy
登陆 http://xxxx:9000 查看是否安装成功, 安装成功按照提示,创建项目可以获取到项目的DSN
。
laravel sentry
安装 sentry/sentry-laravel
包:
$ composer require sentry/sentry-laravel:1.1.0
Laravel 5.5 及以上版本支持自动发现. 否则需要手动配置服务 config/app.php
.
'providers' => array(
// ...
Sentry\Laravel\ServiceProvider::class,
)
'aliases' => array(
// ...
'Sentry' => Sentry\Laravel\Facade::class,
)
增加 sentry
异常上报。修改文件 App/Exceptions/Handler.php
:
public function report(Exception $exception)
{
if (app()->bound('sentry') && $this->shouldReport($exception)){
app('sentry')->captureException($exception);
}
parent::report($exception);
}
通过以下命令创建 (config/sentry.php
) :
$ php artisan vendor:publish --provider="Sentry\Laravel\ServiceProvider"
添加 DSN
到配置文件 .env
:
SENTRY_LARAVEL_DSN=https://<key>@sentry.io/<project>
添加路由进行验证是否成功:
Route::get('/debug-sentry', function () {
throw new Exception('My first Sentry error!');
});
以上只是支持了,代码爆出异常记录到sentry 想要达到日志同步到sentry 还要进行下一步配置。
laravel 5.6 日志录入进sentry
必要条件: PHP7.3
及以上,laravel 5.6
及以上
若从5.5
升级到5.6
, 是不存在config/logging.php
这个配置的,可去 github
laravel/laravel 5.6 分支下复制到本地。
<?php
use Monolog\Handler\StreamHandler;
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'sentry'], //此处增加 sentry channel
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 7,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
//以下为新增channel sentry
'sentry' => [
'driver' => 'sentry',
'level' => \Monolog\Logger::NOTICE,
// The minimum monolog logging level at which this handler will be triggered
// For example: `\Monolog\Logger::ERROR`
'bubble' => true,
// Whether the messages that are handled can bubble up the stack or not
],
],
];
到这里就可以将日志推送到sentry
了, 试一下吧。