Permalink
Browse files

Adds PSR-3 Log adapter

  • Loading branch information...
1 parent 20f4ff3 commit b10a01833ee7408bab43a1d9b79640e3ee5b35d0 @hobodave hobodave committed Mar 20, 2013
Showing with 67 additions and 0 deletions.
  1. +1 −0 composer.json
  2. +43 −0 src/Guzzle/Log/PsrLogAdapter.php
  3. +23 −0 tests/Guzzle/Tests/Log/PsrLogAdapterTest.php
View
@@ -60,6 +60,7 @@
"doctrine/cache": "*",
"symfony/class-loader": "*",
"monolog/monolog": "1.*",
+ "psr/log": "1.0.*",
"zendframework/zend-cache": "2.0.*",
"zendframework/zend-log": "2.0.*",
"zend/zend-log1": "1.12",
@@ -0,0 +1,43 @@
+<?php
+
+namespace Guzzle\Log;
+
+use Psr\Log\LogLevel;
+use Psr\Log\LoggerInterface;
+
+/**
+ * PSR-3 log adapter
+ *
+ * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
+ */
+class PsrLogAdapter extends AbstractLogAdapter
+{
+ /**
+ * syslog to PSR-3 mappings
+ */
+ private static $mapping = array(
+ LOG_DEBUG => LogLevel::DEBUG,
+ LOG_INFO => LogLevel::INFO,
+ LOG_WARNING => LogLevel::WARNING,
+ LOG_ERR => LogLevel::ERROR,
+ LOG_CRIT => LogLevel::CRITICAL,
+ LOG_ALERT => LogLevel::ALERT
+ );
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct(LoggerInterface $logObject)
+ {
+ $this->log = $logObject;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function log($message, $priority = LOG_INFO, $extras = null)
+ {
+
+ $this->log->log(self::$mapping[$priority], $message, $extras);
+ }
+}
@@ -0,0 +1,23 @@
+<?php
+
+namespace Guzzle\Tests\Log;
+
+use Guzzle\Log\MonologLogAdapter;
+use Monolog\Logger;
+use Monolog\Handler\TestHandler;
+
+/**
+ * @covers Guzzle\Log\PsrLogAdapter
+ */
+class PsrLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
+{
+ public function testLogsMessagesToAdaptedObject()
+ {
+ $log = new Logger('test');
+ $handler = new TestHandler();
+ $log->pushHandler($handler);
+ $adapter = new MonologLogAdapter($log);
+ $adapter->log('test!', LOG_INFO);
+ $this->assertTrue($handler->hasInfoRecords());
+ }
+}

0 comments on commit b10a018

Please sign in to comment.