nex / yii2-graylog2
Yii2 的 Graylog2 日志目标
dev-master
2019-07-09 02:50 UTC
Requires
- graylog2/gelf-php: ~1.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 16:13:31 UTC
README
致谢
Benjamin Zikarsky https://github.com/bzikarsky/gelf-php
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require "nex/yii2-graylog2" "*"
或者添加以下内容到你的应用程序的 composer.json
文件的 require
部分:
"nex/yii2-graylog2" : "*"
require
使用方法
将 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 将使用日志消息中的跟踪数组(第一个元素)来设置 file
和 line
gelf 字段。因此,如果你想在这些字段中看到 Graylog2,你需要将 log
组件的 traceLevel
属性设置为 1 或更高。此外,跟踪中的所有行都将作为 trace
附加 gelf 字段发送。
你可以记录不仅是字符串,还可以记录任何其他类型(非字符串将通过 yii\helpers\VarDumper::dumpAsString()
转换为字符串)。
默认情况下,GraylogTarget 将整个日志消息作为 short_message
gelf 字段。但你可以使用 'short'
、'full'
和 'add'
键分别设置 short_message
、full_message
和 additionals
。
<?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', ], ]);