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

如何解决如何使用 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 服务器

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?