nikitich / yii2-graylog-target
Yii2 日志目标用于 Graylog
dev-master
2018-12-11 12:44 UTC
Requires
- graylog2/gelf-php: ~1.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-26 18:02:59 UTC
README
致谢
基于 Benjamin Zikarsky gelf-php 库
灵感来源于 Roman Ovchinnikov yii2-graylog2
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require "nikitich/yii2-graylog-target" "dev-master"
或者
"nikitich/yii2-graylog-target" : "dev-master"
将以下内容添加到您的应用 composer.json
文件的 require
部分。
用法
将 Graylog 目标添加到您的日志组件配置
<?php return [ ... 'components' => [ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ 'file' => [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], 'graylog' => [ 'class' => 'nikitich\graylog\GraylogTarget', 'levels' => ['error', 'warning', 'info'], 'categories' => ['application'], 'logVars' => [], // This prevent yii2-debug from crashing ;) 'server_url' => '127.0.0.1', 'use_udp' => false, // For use HTTP transport 'facility' => 'facility-name', 'add_fields' => [ // Additional fields which must be added 'user-ip' => function($yii) { // to every message (optional) return $yii->request->getUserIP(); // can contain callable function which }, // will recieve instance of Yii application 'tag' => 'tag-name' // as argument ] ], ], ], ], ... ];
GraylogTarget 将使用日志消息中的跟踪数组(第一个元素)来设置 gelf 字段中的 file
和 line
。因此,如果您想在 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', ], ]);