ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 ($persistent)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write timeout. More...
 
 setWritingTimeout ($seconds)
 Set writing 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...
 
 getWritingTimeout ()
 Get current local writing 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)
 
 getResource ()
 
- 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.
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 (!$this->validateStringLength($dataArray['message'], static::MAXIMUM_MESSAGE_LENGTH)) {
147 if (function_exists('mb_substr')) {
148 $dataArray['message'] = mb_substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]';
149 } else {
150 $dataArray['message'] = substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]';
151 }
152 }
153
154 // if we are using the legacy API then we need to send some additional information
155 if ($this->version == self::API_V1) {
156 $dataArray['room_id'] = $this->room;
157 }
158
159 // append the sender name if it is set
160 // always append it if we use the v1 api (it is required in v1)
161 if ($this->version == self::API_V1 || $this->name !== null) {
162 $dataArray['from'] = (string) $this->name;
163 }
164
165 return http_build_query($dataArray);
166 }
$dataArray
getAlertColor($level)
Assigns a color to each level of log records.

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

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 174 of file HipChatHandler.php.

175 {
176 if ($this->version == self::API_V1) {
177 $header = "POST /v1/rooms/message?format=json&auth_token={$this->token} HTTP/1.1\r\n";
178 } else {
179 // needed for rooms with special (spaces, etc) characters in the name
180 $room = rawurlencode($this->room);
181 $header = "POST /v2/room/{$room}/notification?auth_token={$this->token} HTTP/1.1\r\n";
182 }
183
184 $header .= "Host: {$this->host}\r\n";
185 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
186 $header .= "Content-Length: " . strlen($content) . "\r\n";
187 $header .= "\r\n";
188
189 return $header;
190 }
$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 259 of file HipChatHandler.php.

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

References Monolog\Handler\AbstractHandler\$level, $messages, $records, Monolog\Handler\AbstractHandler\getFormatter(), PHP_EOL, 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
int$level
Returns
string

Definition at line 198 of file HipChatHandler.php.

199 {
200 switch (true) {
201 case $level >= Logger::ERROR:
202 return 'red';
203 case $level >= Logger::WARNING:
204 return 'yellow';
205 case $level >= Logger::INFO:
206 return 'green';
207 case $level == Logger::DEBUG:
208 return 'gray';
209 default:
210 return 'yellow';
211 }
212 }
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 228 of file HipChatHandler.php.

229 {
230 if (count($records) == 0) {
231 return true;
232 }
233
234 $batchRecords = $this->combineRecords($records);
235
236 $handled = false;
237 foreach ($batchRecords as $batchRecord) {
238 if ($this->isHandling($batchRecord)) {
239 $this->write($batchRecord);
240 $handled = true;
241 }
242 }
243
244 if (!$handled) {
245 return false;
246 }
247
248 return false === $this->bubble;
249 }
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 342 of file HipChatHandler.php.

343 {
344 if (function_exists('mb_strlen')) {
345 return (mb_strlen($str) <= $length);
346 }
347
348 return (strlen($str) <= $length);
349 }

Referenced by Monolog\Handler\HipChatHandler\__construct(), Monolog\Handler\HipChatHandler\buildContent(), 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 219 of file HipChatHandler.php.

220 {
221 parent::write($record);
222 $this->closeSocket();
223 }
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

Definition at line 64 of file HipChatHandler.php.

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

◆ $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: