php 如何在Laravel应用程序中使用Monolog ElasticSearchlog进行日志记录

t30tvxxf  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(124)

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

kkbh8khc

kkbh8khc1#

这里有一个简短的一步一步的指导如何做到这一点。
1.为你的ElasticSearch日志创建一个配置文件。

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, function ($app) use ($index, $type) {
            return new ElasticsearchHandler($app->make(Client::class), [
                'index'        => $index,
                'type'         => $type,
                'ignore_error' => false,
            ]);
        });
    }
}

将此提供程序添加到您的app.php配置文件中的providers数组中:

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.现在您可以使用通道'elastic'或在.env设置中将其更改为默认通道:

LOG_CHANNEL=elastic

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

相关问题