Monolog 包含弹性搜索处理程序和格式化程序,但它作为自定义通道对 Laravel 的实现并不像 Laravel 文档网站上描述的那么简单。
这是如何执行此操作的简短分步说明。
config/elastic_log.php
接下来的内容:
<?php
return [
'host' => env('ELASTIC_HOST'),
'index' => 'index_name',
'prefix' => 'index_prefix',
'type' => '_doc',
];
您可以将索引名称和前缀更改为任何字符串值。
ELASTIC_HOST=your_elastic_host:port
composer require elasticsearch/elasticsearch
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, function ($app) use ($index, $type) {
return new ElasticsearchHandler($app->make(Client::class), [
'index' => $index,
'type' => $type,
'ignore_error' => false,
]);
});
}
}
将此提供程序添加到您的 app.php 配置文件中的提供程序数组中:
App\Providers\ElasticLogProvider::class,
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,
];
LOG_CHANNEL=elastic
从现在开始,您可以使用标准的 Laravel Log Facade 将信息发送到您的 ElasticSearch 服务器
是否有可能以某种方式在启动方法中获得“名称”,如“pythons”或通道的“重复项”?
例如我在logging.php中定义了两个通道:
'pythons' => [
'driver' => 'monolog',
'handler' => ElasticsearchHandler::class,
'level' => env('LOG_LEVEL', 'debug'),
'formatter' => ElasticsearchFormatter::class
],
'duplicates' => [
'driver' => 'monolog',
'handler' => ElasticsearchHandler::class,
'level' => env('LOG_LEVEL', 'debug'),
'formatter' => ElasticsearchFormatter::class
],
我想要自定义 $type 而不是使用通用类型: $type = config('elastic_log.type');
谢谢你。