laravel 使用 logstash 的日志系统

laravel log system with logstash

我有一个 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 (docs)
  2. 中配置日志通道
    '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),
        ],
    ],
  1. 创建自定义记录器工厂(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 将日志写入指定的主机和端口
  1. 现在要将日志条目写入 logstash,请指定您刚刚创建的日志记录通道 (docs)
Log::channel('logstash')->info('Hello logstash!');

由于 UCP 套接字连接,这对我不起作用,我没有调试它,而是将它更改为 TCP,它就可以工作了。

我先在命令行测试了一下:

echo "hello world!" | nc 127.0.0.1 5000

如果这不起作用,那么您需要更新您的弹性配置,否则 s/udp/tcp。

$handler = new SocketHandler("tcp://127.0.0.1:5000");
$formatter = new LogstashFormatter('EXAMPLE');

$handler->setFormatter($formatter);

$logger =  new Logger('logstash.main', [$handler]);