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

问题描述 投票:0回答:2

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

php laravel elasticsearch logging monolog
2个回答
4
投票

这是如何执行此操作的简短分步说明。

  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, 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,
  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 服务器


0
投票

是否有可能以某种方式在启动方法中获得“名称”,如“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');

谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.