如何解决如何使用 monolog ElasticSearchHandler 登录 Laravel 应用程序
Monolog 包含弹性搜索处理程序和格式化程序,但它作为自定义通道的 Laravel 实现并不像 Laravel 文档网站上描述的那样简单。
解决方法
这里有一个简要的分步说明如何做到这一点。
- 为您的弹性搜索日志创建配置文件。
config/elastic_log.php
接下来的内容:
<?php
return [
'host' => env('ELASTIC_HOST'),'index' => 'index_name','prefix' => 'index_prefix','type' => '_doc',];
您可以将索引名称和前缀更改为任何字符串值。
- 在您的 .env 文件中输入您的弹性主机地址:
ELASTIC_HOST=your_elastic_host:port
- 安装elasticsearch/elasticsearch官方包
composer require elasticsearch/elasticsearch
- 创建 ElasticLogging 服务提供者
php artisan make:provider ElasticLogProvider
有以下内容:
<?php
namespace App\Providers;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Handler\ElasticsearchHandler;
class ElasticLogProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$index = rtrim(config('elastic_log.prefix'),'_') . '_' . config('elastic_log.index');
$type = config('elastic_log.type');
$this->app->bind(Client::class,function ($app) {
return ClientBuilder::create()->setHosts([config('elastic_log.host')])->build();
});
$this->app->bind(ElasticsearchFormatter::class,function ($app) use ($index,$type) {
return new ElasticsearchFormatter($index,$type);
});
$this->app->bind(ElasticsearchHandler::class,$type) {
return new ElasticsearchHandler($app->make(Client::class),[
'index' => $index,'type' => $type,'ignore_error' => false,]);
});
}
}
将此提供程序添加到您的 app.php 配置文件到提供程序数组中:
App\Providers\ElasticLogProvider::class,
- 为服务器上的弹性日志记录设置创建命令。如果它还不存在,此命令会在服务器上创建一个索引。 现在准备您的服务器,只需运行 elastic:log_setup;
php artisan make:command ElasticLogSetup
有以下内容:
<?php
namespace App\Console\Commands;
use Elasticsearch\Client;
use Illuminate\Console\Command;
class ElasticLogSetup extends Command
{
/**
* @var Client
*/
protected $client;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'elastic:log_setup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup elastic log index';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$index = rtrim(config('elastic_log.prefix'),'_') . '_' . config('elastic_log.index');
if (!$this->client->indices()->exists(['index' => $index])) {
$this->client->indices()->create([
'index' => $index,]);
}
}
}
- 在文件
config/logging.php
中,将此元素添加到 'channels' 数组并导入相关类:
use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Handler\ElasticsearchHandler;
'elastic' => [
'driver' => 'monolog','handler' => ElasticsearchHandler::class,'level' => 'debug','formatter' => ElasticsearchFormatter::class,];
- 现在您可以使用“弹性”频道或在您的 .env 设置中将其更改为默认频道:
LOG_CHANNEL=elastic
从现在开始,您可以使用标准的 Laravel Log 门面将信息发送到您的 ElasticSearch 服务器
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。