payprocessing/yii2-graylog2

Yii2 的 Graylog2 日志 TCP 目标

安装: 56

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 4

分支: 24

类型:yii2-extension

v1.0.1 2016-11-17 14:44 UTC

This package is auto-updated.

Last update: 2024-09-18 02:18:51 UTC


README

致谢

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

安装

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

可以运行

php composer.phar require "nex/yii2-graylog2" "*"

或者将以下内容添加到您应用程序的 composer.json 文件的 require 部分:

"nex/yii2-graylog2" : "*"

to

使用

将 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',
                ],
            ],
        ],
    ],
    ...
];

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

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

默认情况下,GraylogTarget 将整个日志消息作为 gelf 字段 short_message。但您可以使用分别使用 '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',
    ],
]);