x-mlex/yii2-graylog2

Yii2 的 Graylog2 日志目标

安装: 347

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 24

类型:yii2-extension

dev-master 2024-09-11 07:54 UTC

This package is auto-updated.

Last update: 2024-09-11 07:55:39 UTC


README

致谢

Benjamin Zikarsky https://github.com/bzikarsky/gelf-php

安装

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

运行以下命令之一:

php composer.phar require "x-mlex/yii2-graylog2" "*"

或添加以下内容到你的应用程序的 composer.json 文件的 require 部分。

"x-mlex/yii2-graylog2" : "*"

使用

将 Graylog 目标添加到你的日志组件配置中

<?php
return [
    ...
    'components' => [
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                'file' => [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                'graylog' => [
                    'class' => 'nex\graylog\GraylogTarget',
                    'levels' => ['error', 'warning', 'info'],
                    'categories' => ['application'],
                    'logVars' => [], // This prevent yii2-debug from crashing ;)
                    'host' => '127.0.0.1',
                    'facility' => 'facility-name',
                    'additionalFields' => [
                        'user-ip' => function($yii) {
                            return $yii->request->getUserIP();
                        },
                        'tag' => 'tag-name'
                    ]
                ],
            ],
        ],
    ],
    ...
];

GraylogTarget 将使用日志消息中的跟踪数组(第一个元素)来设置 gelf 字段中的 fileline。因此,如果你想在 Graylog2 中看到这些字段,你需要将 log 组件的 traceLevel 属性设置为 1 或更多。此外,所有跟踪行都将作为 trace 附加 gelf 字段发送。

你可以记录不仅是字符串,还可以是任何其他类型(非字符串将通过 yii\helpers\VarDumper::dumpAsString() 输出)。

默认情况下,GraylogTarget 将将整个日志消息作为 short_message gelf 字段。但你可以使用 'short''full''add' 键分别设置 short_messagefull_messageadditionals

<?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 'add':
// string representation of ['test1' => 123, 'test2' => 456]
Yii::info([
    'test1' => 123,
    'test2' => 456,
    'short' => 'Test short message',
    'add' => [
        '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',
    'add' => [
        'additional1' => 'abc',
        'additional2' => 'def',
    ],
]);

错误示例

$exception = new \Exception('Test exception');
Yii::info([
    'test1' => 123,
    'test2' => 456,
    'short' => 'Test short message',
    'full' => 'Test full message',
    'add' => [
        'additional1' => 'abc',
        'additional2' => 'def',
    ],
    'exception' => $exception,
]);