ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
GelfHandlerTest.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
14use Gelf\Message;
18
20{
21 public function setUp()
22 {
23 if (!class_exists('Gelf\Publisher') || !class_exists('Gelf\Message')) {
24 $this->markTestSkipped("graylog2/gelf-php not installed");
25 }
26 }
27
31 public function testConstruct()
32 {
33 $handler = new GelfHandler($this->getMessagePublisher());
34 $this->assertInstanceOf('Monolog\Handler\GelfHandler', $handler);
35 }
36
37 protected function getHandler($messagePublisher)
38 {
39 $handler = new GelfHandler($messagePublisher);
40
41 return $handler;
42 }
43
44 protected function getMessagePublisher()
45 {
46 return $this->getMock('Gelf\Publisher', array('publish'), array(), '', false);
47 }
48
49 public function testDebug()
50 {
51 $record = $this->getRecord(Logger::DEBUG, "A test debug message");
52 $expectedMessage = new Message();
53 $expectedMessage
54 ->setLevel(7)
55 ->setFacility("test")
56 ->setShortMessage($record['message'])
57 ->setTimestamp($record['datetime'])
58 ;
59
60 $messagePublisher = $this->getMessagePublisher();
61 $messagePublisher->expects($this->once())
62 ->method('publish')
63 ->with($expectedMessage);
64
65 $handler = $this->getHandler($messagePublisher);
66
67 $handler->handle($record);
68 }
69
70 public function testWarning()
71 {
72 $record = $this->getRecord(Logger::WARNING, "A test warning message");
73 $expectedMessage = new Message();
74 $expectedMessage
75 ->setLevel(4)
76 ->setFacility("test")
77 ->setShortMessage($record['message'])
78 ->setTimestamp($record['datetime'])
79 ;
80
81 $messagePublisher = $this->getMessagePublisher();
82 $messagePublisher->expects($this->once())
83 ->method('publish')
84 ->with($expectedMessage);
85
86 $handler = $this->getHandler($messagePublisher);
87
88 $handler->handle($record);
89 }
90
92 {
93 $record = $this->getRecord(Logger::WARNING, "A test warning message");
94 $record['extra']['blarg'] = 'yep';
95 $record['context']['from'] = 'logger';
96
97 $expectedMessage = new Message();
98 $expectedMessage
99 ->setLevel(4)
100 ->setFacility("test")
101 ->setHost("mysystem")
102 ->setShortMessage($record['message'])
103 ->setTimestamp($record['datetime'])
104 ->setAdditional("EXTblarg", 'yep')
105 ->setAdditional("CTXfrom", 'logger')
106 ;
107
108 $messagePublisher = $this->getMessagePublisher();
109 $messagePublisher->expects($this->once())
110 ->method('publish')
111 ->with($expectedMessage);
112
113 $handler = $this->getHandler($messagePublisher);
114 $handler->setFormatter(new GelfMessageFormatter('mysystem', 'EXT', 'CTX'));
115 $handler->handle($record);
116 }
117}
Serializes a log message to GELF.
testConstruct()
@covers Monolog\Handler\GelfHandler::__construct
Handler to send messages to a Graylog2 (http://www.graylog2.org) server.
Definition: GelfHandler.php:28
Monolog log channel.
Definition: Logger.php:28
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const DEBUG
Detailed debug information.
Definition: Logger.php:32
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19