krisengine / yii2-graylog2
Yii2 的 Graylog2 日志目标
dev-master
2017-10-26 09:27 UTC
Requires
- graylog2/gelf-php: ~1.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-29 04:26:00 UTC
README
致谢
Benjamin Zikarsky https://github.com/bzikarsky/gelf-php
安装
安装此扩展的首选方式是通过 composer。
运行
php composer.phar require "krisengine/yii2-graylog2" "*"
或运行
composer require "krisengine/yii2-graylog2" "*"
或在您的应用程序的 composer.json 文件的 require 部分中添加
"krisengine/yii2-graylog2" : "*"
。
用法
将 Graylog 目标添加到您的日志组件配置中
<?php return [ ... 'components' => [ 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ 'file' => [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], 'graylog' => [ 'class' => 'krisengine\graylog\GraylogTarget', 'levels' => ['error', 'warning', 'info'], 'categories' => ['application'], 'logVars' => [], // This prevent yii2-debug from crashing ;) 'host' => '127.0.0.1', 'facility' => 'facility-name', 'transport' => 'udp', //GELF transport udp, tcp http or https 'sslVerifyPeer' => true, //Enable certificate validation of remote party (GELF transport https) 'sslAllowSelfSigned' => true, //Allow self-signed certificates (GELF transport https) 'sslCaFile' => null, //Path to custom CA (GELF transport https) 'sslCiphers' => null, //List of ciphers the SSL layer may use. Formatted as specified in `ciphers(1)` (GELF transport https) 'httpUsername' => true, //Username for HTTP basic authentication (GELF transport http or https) 'httpPassword' => true, //Password for HTTP basic authentication (GELF transport http or https) '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', ], ]);