yiier/yii2-graylog-target

Yii2 的 Graylog 日志目标

安装次数: 6,406

依赖项: 1

建议者: 0

安全: 0

星标: 2

关注者: 4

分支: 0

开放性问题: 0

类型:yii2-extension

v1.1.0 2020-03-21 11:19 UTC

This package is auto-updated.

Last update: 2024-09-21 22:57:54 UTC


README

Yii2 的 Graylog 日志目标

Latest Stable Version Total Downloads Latest Unstable Version License

安装

安装此扩展的首选方式是通过 composer.

运行以下命令之一

php composer.phar require --prefer-dist yiier/yii2-graylog-target "*"

或者将以下内容添加到您的 composer.json 文件的 require 部分。

"yiier/yii2-graylog-target": "*"

配置

return [
    'components' => [
        'log' => [
            'targets' => [
                'graylog' => [
                    'class' => yiier\graylog\Target::class,
                    'levels' => ['error', 'warning', 'info'],
                    // 'categories' => ['application', 'graylog'],
                    // 'logVars' => ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION'],
                    // 'facility' => 'facility-name',
                    'transport' => [
                        'class' => yiier\graylog\transport\UdpTransport::class,
                        'host' => '127.0.0.1',
                        'port' => '1231',
                        'chunkSize' => 4321,
                    ],
                    'additionalFields' => [
                        'user-ip' => function ($yii) {
                            return ($yii instanceof \yii\console\Application) ? '' : $yii->request->userIP;
                        },
                        'tag' => 'tag-name'
                    ],
                ],
            ],
        ],
    ],
];

传输方式

UDP 传输

$transport = new yiier\graylog\transport\UdpTransport([
    // Host name or IP. Default to 127.0.0.1
    'host' => 'graylog.example.org',
    // UDP port. Default to 12201
    'port' => 1234,
    // UDP chunk size. Default to 8154
    'chunkSize' => 4321,
]);

TCP 传输

$transport = new yiier\graylog\transport\UdpTransport([
    // Host name or IP. Default to 127.0.0.1
    'host' => 'graylog.example.org',
    // TCP port. Default to 12201
    'port' => 12201,
    // SSL options. (optional)
    'sslOptions' => [
        // Default to true
        'verifyPeer' => false,
        // Default to false
        'allowSelfSigned' => true,
        // Default to null
        'caFile' => '/path/to/ca.file',
        // Default to null
        'ciphers' => 'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256',
    ],
]);

HTTP 传输

$transport = new yiier\graylog\transport\HttpTransport([
    // Host name or IP. Default to 127.0.0.1
    'host' => 'graylog.example.org',
    // HTTP port. Default to 12202
    'port' => 12202,
    // Query path. Default to /gelf
    'path' => '/my/custom/greylog',
    // SSL options. (optional)
    'sslOptions' => [
        // Default to true
        'verifyPeer' => false,
        // Default to false
        'allowSelfSigned' => true,
        // Default to null
        'caFile' => '/path/to/ca.file',
        // Default to null
        'ciphers' => 'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256',
    ],
]);

使用方法

扩展安装完成后,只需在代码中通过以下方式使用它:

使用 \yiier\graylog\Log::info

<?php
\yiier\graylog\Log::info(
    'Test short message',
    'Test full message'
);


\yiier\graylog\Log::info(
    'Test short message',
    'Test full message', 
    [
        'additional1' => 'abc',
        'additional2' => 'def',
    ],
    'graylog'
);

使用 Yii::info

<?php
// short_message will contain string representation of ['test1' => 123, 'test2' => 456],
// no full_message will be sent
Yii::info([
    'test1' => 123,
    'test2' => 456,
]);

// short_message will contain 'Test short message',
// two additional fields will be sent,
// full_message will contain all other stuff without 'short' and 'additional':
// string representation of ['test1' => 123, 'test2' => 456]
Yii::info([
    'test1' => 123,
    'test2' => 456,
    'short' => 'Test short message',
    'additional' => [
        'additional1' => 'abc',
        'additional2' => 'def',
    ],
]);
 
// short_message will contain 'Test short message',
// two additional fields will be sent,
// full_message will contain 'Test full message', all other stuff will be lost
Yii::info([
    'test1' => 123,
    'test2' => 456,
    'short' => 'Test short message',
    'full' => 'Test full message',
    'additional' => [
        'additional1' => 'abc',
        'additional2' => 'def',
    ],
]);

参考