Skip to content

Instantly share code, notes, and snippets.

@iainp999
Last active August 29, 2015 14:14
Show Gist options
  • Save iainp999/250033b61c4ebb02ed40 to your computer and use it in GitHub Desktop.
Save iainp999/250033b61c4ebb02ed40 to your computer and use it in GitHub Desktop.
Guzzle log adaptor for Drupal
<?php
/**
* Log messages using Drupal watchdog.
*/
class DrupalLogAdaptor implements \Guzzle\Log\LogAdapterInterface {
private $type = 'guzzle';
/**
* @param string $type
*/
function __construct($type = 'guzzle') {
$this->type = $type;
}
/**
* Log a message at a priority
*
* @param string $message Message to log
* @param integer $priority Priority of message (use the \LOG_* constants of 0 - 7)
* @param array $extras Extra information to log in event
*/
public function log($message, $priority = LOG_INFO, $extras = array()) {
watchdog($this->type, $message, $variables = array(), $this->mapPriority($priority));
}
/**
* Map a Guzzle priority to a Drupal severity.
*/
protected function mapPriority($priority) {
switch($priority) {
case LOG_ALERT:
return WATCHDOG_ALERT;
case LOG_CRIT:
return WATCHDOG_CRITICAL;
case LOG_DEBUG:
return WATCHDOG_DEBUG;
case LOG_EMERG:
return WATCHDOG_EMERGENCY;
case LOG_ERR:
return WATCHDOG_ERROR;
case LOG_INFO:
return WATCHDOG_INFO;
case LOG_WARNING:
return WATCHDOG_WARNING;
default:
return WATCHDOG_NOTICE;
}
}
}
@iainp999
Copy link
Author

iainp999 commented Feb 2, 2015

e.g.

$adapter = new DrupalLogAdapter();
$logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT);
$client->addSubscriber($logPlugin);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment