ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Monolog\Handler\HipChatHandler Class Reference

Sends notifications through the hipchat api to a hipchat room. More...

+ Inheritance diagram for Monolog\Handler\HipChatHandler:
+ Collaboration diagram for Monolog\Handler\HipChatHandler:

Public Member Functions

 __construct ($token, $room, $name='Monolog', $notify=false, $level=Logger::CRITICAL, $bubble=true, $useSSL=true, $format='text', $host='api.hipchat.com', $version=self::API_V1)
 
 handleBatch (array $records)
 {{Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
}} More...
 
- Public Member Functions inherited from Monolog\Handler\SocketHandler
 __construct ($connectionString, $level=Logger::DEBUG, $bubble=true)
 
 close ()
 We will not close a PersistentSocket instance so it can be reused in other requests. More...
 
 closeSocket ()
 Close socket, if open. More...
 
 setPersistent ($boolean)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write timeout. More...
 
 getConnectionString ()
 Get current connection string. More...
 
 isPersistent ()
 Get persistent setting. More...
 
 getConnectionTimeout ()
 Get current connection timeout setting. More...
 
 getTimeout ()
 Get current in-transfer timeout. More...
 
 isConnected ()
 Check to see if the socket is currently available. More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 handle (array $record)
 {Handles a record.All records may be passed to this method, and the handler should discard those that it does not want to handle.The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.
Parameters
array$recordThe record to handle
Returns
Boolean true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
} More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
Boolean
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 
 isHandling (array $record)
 Checks whether the given record will be handled by this handler. More...
 
 handle (array $record)
 Handles a record. More...
 
 handleBatch (array $records)
 Handles a set of records at once. More...
 
 pushProcessor ($callback)
 Adds a processor in the stack. More...
 
 popProcessor ()
 Removes the processor on top of the stack and returns it. More...
 
 setFormatter (FormatterInterface $formatter)
 Sets the formatter. More...
 
 getFormatter ()
 Gets the formatter. More...
 

Data Fields

const API_V1 = 'v1'
 Use API version 1. More...
 
const API_V2 = 'v2'
 Use API version v2. More...
 
const MAXIMUM_NAME_LENGTH = 15
 The maximum allowed length for the name used in the "from" field. More...
 
const MAXIMUM_MESSAGE_LENGTH = 9500
 The maximum allowed length for the message. More...
 

Protected Member Functions

 generateDataStream ($record)
 {} More...
 
 getAlertColor ($level)
 Assigns a color to each level of log records. More...
 
 write (array $record)
 {Connect (if necessary) and write to the socket.
Parameters
array$record
Exceptions

UnexpectedValueException

Exceptions

RuntimeException} More...

 
- Protected Member Functions inherited from Monolog\Handler\SocketHandler
 write (array $record)
 Connect (if necessary) and write to the socket. More...
 
 pfsockopen ()
 Wrapper to allow mocking. More...
 
 fsockopen ()
 Wrapper to allow mocking. More...
 
 streamSetTimeout ()
 Wrapper to allow mocking. More...
 
 fwrite ($data)
 Wrapper to allow mocking. More...
 
 streamGetMetadata ()
 Wrapper to allow mocking. More...
 
 generateDataStream ($record)
 
- Protected Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 write (array $record)
 Writes the record down to the log of the implementing handler. More...
 
 processRecord (array $record)
 Processes a record. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Private Member Functions

 buildContent ($record)
 Builds the body of API call. More...
 
 buildHeader ($content)
 Builds the header of the API Call. More...
 
 combineRecords ($records)
 Combines multiple records into one. More...
 
 validateStringLength ($str, $length)
 Validates the length of a string. More...
 

Private Attributes

 $token
 
 $room
 
 $name
 
 $notify
 
 $format
 
 $host
 
 $version
 

Additional Inherited Members

- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Detailed Description

Sends notifications through the hipchat api to a hipchat room.

Notes: API token - HipChat API token Room - HipChat Room Id or name, where messages are sent Name - Name used to send the message (from) notify - Should the message trigger a notification in the clients version - The API version to use (HipChatHandler::API_V1 | HipChatHandler::API_V2)

Author
Rafael Dohms rafae.nosp@m.l@do.nosp@m.h.ms
See also
https://www.hipchat.com/docs/api

Definition at line 29 of file HipChatHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\HipChatHandler::__construct (   $token,
  $room,
  $name = 'Monolog',
  $notify = false,
  $level = Logger::CRITICAL,
  $bubble = true,
  $useSSL = true,
  $format = 'text',
  $host = 'api.hipchat.com',
  $version = self::API_V1 
)
Parameters
string$tokenHipChat API Token
string$roomThe room that should be alerted of the message (Id or Name)
string$nameName used in the "from" field. Not used for v2
bool$notifyTrigger a notification in clients or not
int$levelThe minimum logging level at which this handler will be triggered
bool$bubbleWhether the messages that are handled can bubble up the stack or not
bool$useSSLWhether to connect via SSL.
string$formatThe format of the messages (default to text, can be set to html if you have html in the messages)
string$hostThe HipChat server hostname.
string$versionThe HipChat API version (default HipChatHandler::API_V1)

Definition at line 98 of file HipChatHandler.php.

99 {
100 if ($version == self::API_V1 && !$this->validateStringLength($name, static::MAXIMUM_NAME_LENGTH)) {
101 throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.');
102 }
103
104 $connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80';
105 parent::__construct($connectionString, $level, $bubble);
106
107 $this->token = $token;
108 $this->name = $name;
109 $this->notify = $notify;
110 $this->room = $room;
111 $this->format = $format;
112 $this->host = $host;
113 $this->version = $version;
114 }
validateStringLength($str, $length)
Validates the length of a string.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\SocketHandler\$connectionString, Monolog\Handler\HipChatHandler\$format, Monolog\Handler\HipChatHandler\$host, Monolog\Handler\AbstractHandler\$level, Monolog\Handler\HipChatHandler\$name, Monolog\Handler\HipChatHandler\$notify, Monolog\Handler\HipChatHandler\$room, Monolog\Handler\HipChatHandler\$token, Monolog\Handler\HipChatHandler\$version, and Monolog\Handler\HipChatHandler\validateStringLength().

+ Here is the call graph for this function:

Member Function Documentation

◆ buildContent()

Monolog\Handler\HipChatHandler::buildContent (   $record)
private

Builds the body of API call.

Parameters
array$record
Returns
string

Definition at line 135 of file HipChatHandler.php.

136 {
137 $dataArray = array(
138 'notify' => $this->version == self::API_V1 ?
139 ($this->notify ? 1 : 0) :
140 ($this->notify ? 'true' : 'false'),
141 'message' => $record['formatted'],
142 'message_format' => $this->format,
143 'color' => $this->getAlertColor($record['level']),
144 );
145
146 // if we are using the legacy API then we need to send some additional information
147 if ($this->version == self::API_V1) {
148 $dataArray['room_id'] = $this->room;
149 $dataArray['from'] = $this->name;
150 }
151
152 return http_build_query($dataArray);
153 }
getAlertColor($level)
Assigns a color to each level of log records.

References Monolog\Handler\HipChatHandler\$name, Monolog\Handler\HipChatHandler\$room, and Monolog\Handler\HipChatHandler\getAlertColor().

Referenced by Monolog\Handler\HipChatHandler\generateDataStream().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buildHeader()

Monolog\Handler\HipChatHandler::buildHeader (   $content)
private

Builds the header of the API Call.

Parameters
string$content
Returns
string

Definition at line 161 of file HipChatHandler.php.

162 {
163 if ($this->version == self::API_V1) {
164 $header = "POST /v1/rooms/message?format=json&auth_token={$this->token} HTTP/1.1\r\n";
165 } else {
166 // needed for rooms with special (spaces, etc) characters in the name
167 $room = rawurlencode($this->room);
168 $header = "POST /v2/room/{$room}/notification?auth_token={$this->token} HTTP/1.1\r\n";
169 }
170
171 $header .= "Host: {$this->host}\r\n";
172 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
173 $header .= "Content-Length: " . strlen($content) . "\r\n";
174 $header .= "\r\n";
175
176 return $header;
177 }
$header

References $header, and Monolog\Handler\HipChatHandler\$room.

Referenced by Monolog\Handler\HipChatHandler\generateDataStream().

+ Here is the caller graph for this function:

◆ combineRecords()

Monolog\Handler\HipChatHandler::combineRecords (   $records)
private

Combines multiple records into one.

Error level of the combined record will be the highest level from the given records. Datetime will be taken from the first record.

Parameters
$records
Returns
array

Definition at line 246 of file HipChatHandler.php.

247 {
248 $batchRecord = null;
249 $batchRecords = array();
250 $messages = array();
251 $formattedMessages = array();
252 $level = 0;
253 $levelName = null;
254 $datetime = null;
255
256 foreach ($records as $record) {
257 $record = $this->processRecord($record);
258
259 if ($record['level'] > $level) {
260 $level = $record['level'];
261 $levelName = $record['level_name'];
262 }
263
264 if (null === $datetime) {
265 $datetime = $record['datetime'];
266 }
267
268 $messages[] = $record['message'];
269 $messageStr = implode(PHP_EOL, $messages);
270 $formattedMessages[] = $this->getFormatter()->format($record);
271 $formattedMessageStr = implode('', $formattedMessages);
272
273 $batchRecord = array(
274 'message' => $messageStr,
275 'formatted' => $formattedMessageStr,
276 'context' => array(),
277 'extra' => array(),
278 );
279
280 if (!$this->validateStringLength($batchRecord['formatted'], static::MAXIMUM_MESSAGE_LENGTH)) {
281 // Pop the last message and implode the remaining messages
282 $lastMessage = array_pop($messages);
283 $lastFormattedMessage = array_pop($formattedMessages);
284 $batchRecord['message'] = implode(PHP_EOL, $messages);
285 $batchRecord['formatted'] = implode('', $formattedMessages);
286
287 $batchRecords[] = $batchRecord;
288 $messages = array($lastMessage);
289 $formattedMessages = array($lastFormattedMessage);
290
291 $batchRecord = null;
292 }
293 }
294
295 if (null !== $batchRecord) {
296 $batchRecords[] = $batchRecord;
297 }
298
299 // Set the max level and datetime for all records
300 foreach ($batchRecords as &$batchRecord) {
301 $batchRecord = array_merge(
302 $batchRecord,
303 array(
304 'level' => $level,
305 'level_name' => $levelName,
306 'datetime' => $datetime
307 )
308 );
309 }
310
311 return $batchRecords;
312 }
getFormatter()
{Gets the formatter.FormatterInterface}
processRecord(array $record)
Processes a record.
$messages
Definition: en-x-test.php:7
$records
Definition: simple_test.php:17

References Monolog\Handler\AbstractHandler\$level, $messages, $records, Monolog\Handler\AbstractHandler\getFormatter(), Monolog\Handler\AbstractProcessingHandler\processRecord(), and Monolog\Handler\HipChatHandler\validateStringLength().

Referenced by Monolog\Handler\HipChatHandler\handleBatch().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateDataStream()

Monolog\Handler\HipChatHandler::generateDataStream (   $record)
protected

{}

Parameters
array$record
Returns
string

Reimplemented from Monolog\Handler\SocketHandler.

Definition at line 122 of file HipChatHandler.php.

123 {
124 $content = $this->buildContent($record);
125
126 return $this->buildHeader($content) . $content;
127 }
buildContent($record)
Builds the body of API call.
buildHeader($content)
Builds the header of the API Call.

References Monolog\Handler\HipChatHandler\buildContent(), and Monolog\Handler\HipChatHandler\buildHeader().

+ Here is the call graph for this function:

◆ getAlertColor()

Monolog\Handler\HipChatHandler::getAlertColor (   $level)
protected

Assigns a color to each level of log records.

Parameters
integer$level
Returns
string

Definition at line 185 of file HipChatHandler.php.

186 {
187 switch (true) {
188 case $level >= Logger::ERROR:
189 return 'red';
190 case $level >= Logger::WARNING:
191 return 'yellow';
192 case $level >= Logger::INFO:
193 return 'green';
194 case $level == Logger::DEBUG:
195 return 'gray';
196 default:
197 return 'yellow';
198 }
199 }
const ERROR
Runtime errors.
Definition: Logger.php:57
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const INFO
Interesting events.
Definition: Logger.php:39
const DEBUG
Detailed debug information.
Definition: Logger.php:32

References Monolog\Handler\AbstractHandler\$level, Monolog\Logger\DEBUG, Monolog\Logger\ERROR, Monolog\Logger\INFO, and Monolog\Logger\WARNING.

Referenced by Monolog\Handler\HipChatHandler\buildContent().

+ Here is the caller graph for this function:

◆ handleBatch()

Monolog\Handler\HipChatHandler::handleBatch ( array  $records)

{{Handles a set of records at once.

Parameters
array$recordsThe records to handle (an array of record arrays)
}}

Reimplemented from Monolog\Handler\AbstractHandler.

Definition at line 215 of file HipChatHandler.php.

216 {
217 if (count($records) == 0) {
218 return true;
219 }
220
221 $batchRecords = $this->combineRecords($records);
222
223 $handled = false;
224 foreach ($batchRecords as $batchRecord) {
225 if ($this->isHandling($batchRecord)) {
226 $this->write($batchRecord);
227 $handled = true;
228 }
229 }
230
231 if (!$handled) {
232 return false;
233 }
234
235 return false === $this->bubble;
236 }
isHandling(array $record)
{Checks whether the given record will be handled by this handler.This is mostly done for performance ...
write(array $record)
{Connect (if necessary) and write to the socket.UnexpectedValueException RuntimeException}
combineRecords($records)
Combines multiple records into one.

References Monolog\Handler\AbstractHandler\$bubble, $records, Monolog\Handler\HipChatHandler\combineRecords(), Monolog\Handler\AbstractHandler\isHandling(), and Monolog\Handler\HipChatHandler\write().

+ Here is the call graph for this function:

◆ validateStringLength()

Monolog\Handler\HipChatHandler::validateStringLength (   $str,
  $length 
)
private

Validates the length of a string.

If the mb_strlen() function is available, it will use that, as HipChat allows UTF-8 characters. Otherwise, it will fall back to strlen().

Note that this might cause false failures in the specific case of using a valid name with less than 16 characters, but 16 or more bytes, on a system where mb_strlen() is unavailable.

Parameters
string$str
int$length
Returns
bool

Definition at line 329 of file HipChatHandler.php.

330 {
331 if (function_exists('mb_strlen')) {
332 return (mb_strlen($str) <= $length);
333 }
334
335 return (strlen($str) <= $length);
336 }

Referenced by Monolog\Handler\HipChatHandler\__construct(), and Monolog\Handler\HipChatHandler\combineRecords().

+ Here is the caller graph for this function:

◆ write()

Monolog\Handler\HipChatHandler::write ( array  $record)
protected

{Connect (if necessary) and write to the socket.

Parameters
array$record
Exceptions

UnexpectedValueException

Exceptions

RuntimeException}

Parameters
array$record

Reimplemented from Monolog\Handler\SocketHandler.

Definition at line 206 of file HipChatHandler.php.

207 {
208 parent::write($record);
209 $this->closeSocket();
210 }
closeSocket()
Close socket, if open.

References Monolog\Handler\SocketHandler\closeSocket().

Referenced by Monolog\Handler\HipChatHandler\handleBatch().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $format

Monolog\Handler\HipChatHandler::$format
private

Definition at line 74 of file HipChatHandler.php.

Referenced by Monolog\Handler\HipChatHandler\__construct().

◆ $host

Monolog\Handler\HipChatHandler::$host
private

Definition at line 79 of file HipChatHandler.php.

Referenced by Monolog\Handler\HipChatHandler\__construct().

◆ $name

Monolog\Handler\HipChatHandler::$name
private

◆ $notify

Monolog\Handler\HipChatHandler::$notify
private

Definition at line 69 of file HipChatHandler.php.

Referenced by Monolog\Handler\HipChatHandler\__construct().

◆ $room

◆ $token

Monolog\Handler\HipChatHandler::$token
private

Definition at line 54 of file HipChatHandler.php.

Referenced by Monolog\Handler\HipChatHandler\__construct().

◆ $version

Monolog\Handler\HipChatHandler::$version
private

Definition at line 84 of file HipChatHandler.php.

Referenced by Monolog\Handler\HipChatHandler\__construct().

◆ API_V1

const Monolog\Handler\HipChatHandler::API_V1 = 'v1'

Use API version 1.

Definition at line 34 of file HipChatHandler.php.

◆ API_V2

const Monolog\Handler\HipChatHandler::API_V2 = 'v2'

Use API version v2.

Definition at line 39 of file HipChatHandler.php.

◆ MAXIMUM_MESSAGE_LENGTH

const Monolog\Handler\HipChatHandler::MAXIMUM_MESSAGE_LENGTH = 9500

The maximum allowed length for the message.

Definition at line 49 of file HipChatHandler.php.

◆ MAXIMUM_NAME_LENGTH

const Monolog\Handler\HipChatHandler::MAXIMUM_NAME_LENGTH = 15

The maximum allowed length for the name used in the "from" field.

Definition at line 44 of file HipChatHandler.php.


The documentation for this class was generated from the following file: