debugger84 / zf2-graylog
A Zend\Log writer and formatter helps you add Graylog logging to your ZF2 application.
2.2.0
2017-07-03 09:12 UTC
Requires
- graylog2/gelf-php: ^1.5
- psr/container: ^1.0
- zendframework/zendframework: ^2.4
This package is not auto-updated.
Last update: 2024-09-29 03:25:54 UTC
README
A Zend\Log
writer and formatter helps you add Graylog logging to your ZF2 application, using bzikarsky/gelf-php: the newer version of deprecated Graylog2/gelf-php library.
安装
composer require debugger84/zf2-graylog:dev-master
将 graylog 连接的值添加到您的配置中
'graylog' => [
'host' => 'http://graylog.local',
'port' => 12222,
'facility' => 'Your project name'
],
Rewrite factory for Zend Logger interface
\Zend\Log\LoggerInterface::class => \ZF2Graylog\Factory\LoggerFactory::class,
使用方法
我在一个自定义的基控制器中添加了一个方法
class Base extends \Zend\Mvc\Controller\AbstractActionController {
private $logger;
protected function getLogger() {
if (!isset($this->logger)) {
$this->logger = new \Zend\Log\Logger;
// __construct($facility, $hostname, $port)
$writer = new \ZF2Graylog\Log\Writer\Graylog2('ZF2', '127.0.0.1');
$this->logger->addWriter($writer);
}
return $this->logger;
}
}
然后可以像平常一样使用
$this->getLogger()->info('Informative message');
格式化器应该(或将会)支持错误日志(如下所示),通过将以下属性映射到相应的 GELF 字段
$logger = new \Zend\Log\Logger;
$writer = new \ZF2Graylog\Log\Writer\Graylog2('ZF2', '127.0.0.1');
$logger->addWriter($writer);
Zend\Log\Logger::registerErrorHandler($logger);
Zend\Log GELFMessage
--------------------------------------------------------------------------------
message message, full_message, short_message (unless present in $values)
errno level
file file
line line
context additional fields (to be tested)
与 GELF 字段关联的附加字段
full
(消息)映射到full_message
(如果存在,否则message
)short
(消息)映射到short_message
(如果存在,否则message
)version
映射到version
(如果不存在则不设置)facility
映射到facility
。在 writer 中设置了默认的 facility。- 附加字段映射为附加字段。
待办事项
- 编写一个包含 Graylog2/gelf-php 库的 composer.json。
- Docblocks。
- 对不同事件的进一步测试。
- 检查 errno 映射到正确的严重级别时用作 errorHandler。
许可协议
TBC,可能会是 MIT。