gpt4 book ai didi

php - Laravel 日志系统与 Logstash

转载 作者:行者123 更新时间:2023-12-02 22:16:27 25 4
gpt4 key购买 nike

我有一个 Laravel 应用程序,我想将日志存储到 Logstash 中并在 kibana 中查看它们,我在网上搜索了很多以找到解决方案,但我没有找到任何好的来源,是否有任何包使用 laravel 登录logstash ???顺便说一句,我的 Logstash 和 kibana 运行没有任何问题,我现在只需要一个数据源。这是我正在运行的 elstic 搜索:

    {
name: "5351ced3b7a4",
cluster_name: "elasticsearch",
cluster_uuid: "Ej5TRN8CQyGvemZlT3gAFA",
version: {
number: "7.1.1",
build_flavor: "oss",
build_type: "tar",
build_hash: "7a013de",
build_date: "2019-05-23T14:04:00.380842Z",
build_snapshot: false,
lucene_version: "8.0.0",
minimum_wire_compatibility_version: "6.8.0",
minimum_index_compatibility_version: "6.0.0-beta1"
},
tagline: "You Know, for Search"
}

编辑正如答案解释了我所做的那样,现在我在 laravel 的日志中收到此错误:

[2019-08-05 14:16:17] laravel.INFO: Hello logstash!  

编辑:日志配置文件:

<?php

use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/

'default' => env('LOG_CHANNEL', 'stack'),

/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/

'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],

'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],

'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],

'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],

'papertrail' => [
'driver' => 'monolog',
'level' => 'debug',
'handler' => SyslogUdpHandler::class,
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
],
],

'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],

'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],

'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
'logstash' => [
'driver' => 'custom',
'via' => \App\LogstashLogger::class,
'host' => env('LOGSTASH_HOST', '127.0.0.1'),
'port' => env('LOGSTASH_PORT', 9200),
],
],

];

最佳答案

this post 中所述这很容易做到。

  1. config/logging.php 中配置日志记录 channel ( docs )
    'channels' => [
// ... other channels like stack or single
'logstash' => [
'driver' => 'custom',
'via' => \App\LogstashLogger::class,
'host' => env('LOGSTASH_HOST', '127.0.0.1'),
'port' => env('LOGSTASH_PORT', 4718),
],
],
  • 创建自定义 Logger 工厂 ( docs )
  • namespace App;

    use Monolog\Formatter\LogstashFormatter;
    use Monolog\Handler\SocketHandler;
    use Monolog\Logger;
    use Psr\Log\LoggerInterface;
    class LogstashLogger {
    /**
    * @param array $config
    * @return LoggerInterface
    */
    public function __invoke(array $config): LoggerInterface
    {
    $handler = new SocketHandler("udp://{$config['host']}:{$config['port']}");
    $handler->setFormatter(new LogstashFormatter(config('app.name')));
    return new Logger('logstash.main', [$handler]);
    }
    }

    这将使用 monolog logstash formatter 通过 udp 将日志写入指定的主机和端口

  • 现在要将日志条目写入logstash,请指定您刚刚创建的日志记录 channel ( docs )
  • Log::channel('logstash')->info('Hello logstash!');

    关于php - Laravel 日志系统与 Logstash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346805/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com