首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带对数的laravel日志系统

带对数的laravel日志系统
EN

Stack Overflow用户
提问于 2019-08-04 13:05:32
回答 2查看 9.3K关注 0票数 2

我有一个laravel应用程序,我想把我的日志存储在我的日志中,并看到他们在基巴纳,我搜索了很多,为它寻找一个解决方案,但我没有找到任何好的来源,有任何包装使用拉拉日志进入日志?顺便说一下,我的日志和基班纳运行时没有任何问题,我现在只需要一个数据源。下面是我的私密搜索:

代码语言:javascript
复制
    {
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编写的日志中得到了这个错误:

代码语言:javascript
复制
[2019-08-05 14:16:17] laravel.INFO: Hello logstash!  

编辑: Logging Config文件:

代码语言:javascript
复制
<?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),
    ],
],

];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-04 15:20:57

正如在这个职位中解释的那样,这很容易做到。

  1. config/logging.php (文档)中配置日志记录通道
代码语言:javascript
复制
    '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. 创建自定义日志工厂(文档)
代码语言:javascript
复制
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]);
    }
}

这将使用单对数存储格式程序通过udp将日志写入指定的主机和端口。

  1. 现在要将日志条目写入logstash,请指定您刚才创建的日志记录通道(文档)
代码语言:javascript
复制
Log::channel('logstash')->info('Hello logstash!');
票数 5
EN

Stack Overflow用户

发布于 2019-11-22 06:00:00

这并不适用于我,因为UCP套接字连接,而不是调试它,我只是将它更改为TCP,它工作了。

我首先在命令行上测试了它:

代码语言:javascript
复制
echo "hello world!" | nc 127.0.0.1 5000

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

代码语言:javascript
复制
$handler = new SocketHandler("tcp://127.0.0.1:5000");
$formatter = new LogstashFormatter('EXAMPLE');

$handler->setFormatter($formatter);

$logger =  new Logger('logstash.main', [$handler]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57346805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档