如何使用 monolog ElasticSearchHandler 登录 Laravel 应用程序

问题描述

Monolog 包含弹性搜索处理程序和格式化程序,但它作为自定义通道的 Laravel 实现并不像 Laravel 文档网站上描述的那样简单。

解决方法

这里有一个简要的分步说明如何做到这一点。

  1. 为您的弹性搜索日志创建配置文件。
config/elastic_log.php

接下来的内容:

<?php

return [
    'host' => env('ELASTIC_HOST'),'index' => 'index_name','prefix' => 'index_prefix','type' => '_doc',];

您可以将索引名称和前缀更改为任何字符串值。

  1. 在您的 .env 文件中输入您的弹性主机地址:
ELASTIC_HOST=your_elastic_host:port
  1. 安装elasticsearch/elasticsearch官方包
composer require elasticsearch/elasticsearch
  1. 创建 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,
  1. 为服务器上的弹性日志记录设置创建命令。如果它还不存在,此命令会在服务器上创建一个索引。 现在准备您的服务器,只需运行 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,]);
        }
    }
}

  1. 在文件 config/logging.php 中,将此元素添加到 'channels' 数组并导入相关类:
use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Handler\ElasticsearchHandler;

'elastic' => [
    'driver' => 'monolog','handler' => ElasticsearchHandler::class,'level' => 'debug','formatter' => ElasticsearchFormatter::class,];
  1. 现在您可以使用“弹性”频道或在您的 .env 设置中将其更改为默认频道:
LOG_CHANNEL=elastic

从现在开始,您可以使用标准的 Laravel Log 门面将信息发送到您的 ElasticSearch 服务器

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...