Skip to content

Commit

Permalink
Adding logging functionality, requires monolog to be installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Mar 2, 2014
1 parent 728e3b3 commit 857fde8
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ $client->setCaching(true, '/tmp/php-tmdb-api';

_This relies on max-age headers being sent back from TMDB, see the [documentation of the CachePlugin](http://guzzle.readthedocs.org/en/latest/plugins/cache-plugin.html)._

If you want to add some logging capabilities ( currently an implementation of the `GuzzleLogPlugin`, requires `monolog/monolog`);

```php
$client->setLogging(true, '/tmp/php-tmdb-api.log';
```

Then we do some work on it:

```php
Expand Down Expand Up @@ -114,6 +120,12 @@ $client->setCaching(true, '/tmp/php-tmdb-api';

_This relies on max-age headers being sent back from TMDB, see the [documentation of the CachePlugin](http://guzzle.readthedocs.org/en/latest/plugins/cache-plugin.html)._

If you want to add some logging capabilities ( currently an implementation of the `GuzzleLogPlugin`, requires `monolog/monolog`);

```php
$client->setLogging(true, '/tmp/php-tmdb-api.log';
```

Then you pass this client onto one of the many repositories and do some work on it.

```php
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
},
"require-dev": {
"phpunit/phpunit": ">=3.7",
"doctrine/cache": ">=1.3.0"
"doctrine/cache": ">=1.3.0",
"monolog/monolog": ">=1.7.0"
},
"suggest": {
"doctrine/cache": "This library is required if you want to make use of caching features."
"doctrine/cache": "This library is required if you want to make use of caching features.",
"monolog/monolog": "This library is required if you want to make use of logging features."
},
"autoload": {
"psr-0": { "Tmdb\\": "lib/" }
Expand Down
22 changes: 22 additions & 0 deletions examples/logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once '../vendor/autoload.php';
require_once '../apikey.php';

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$client->setLogging(true, '/www/dev/php-tmdb-api/api.log');

$repository = new \Tmdb\Repository\MovieRepository($client);
$movie = $repository->load(19995);
111 changes: 110 additions & 1 deletion lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
use Guzzle\Common\HasDispatcherInterface;
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Http\ClientInterface;
use Guzzle\Log\MessageFormatter;
use Guzzle\Log\PsrLogAdapter;
use Guzzle\Plugin\Backoff\BackoffPlugin;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\DefaultCacheStorage;
use Guzzle\Plugin\Log\LogPlugin;
use Tmdb\HttpClient\HttpClient;
use Tmdb\HttpClient\HttpClientInterface;
use Tmdb\ApiToken as Token;
Expand Down Expand Up @@ -76,6 +79,25 @@ class Client
*/
private $httpClient;

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* Holds the log path
*
* @var string
*/
private $logPath;

/**
* Enable logging?
*
* @var bool
*/
private $logEnabled = false;

/**
* Stores the cache path
*
Expand Down Expand Up @@ -148,6 +170,35 @@ private function constructHttpClient(ClientInterface $httpClient = null)
$httpClient->addSubscriber($cachePlugin);
}

if ($this->logEnabled && !empty($this->logPath)) {
if (empty($this->logger) && !class_exists('\Monolog\Logger')) {
/** @codeCoverageIgnoreStart */
throw new RuntimeException(
'Could not find any logger set and the monolog logger library was not found
to provide a default, you have to set a custom logger on the client or
have you forgot adding monolog to your composer.json?'
);
/** @codeCoverageIgnoreEnd */
} else {
$this->logger = new \Monolog\Logger('php-tmdb-api');
$this->logger->pushHandler(
new \Monolog\Handler\StreamHandler(
$this->logPath,
\Monolog\Logger::DEBUG
)
);
}

if ($this->logger instanceof \Psr\Log\LoggerInterface) {
$logPlugin = new LogPlugin(
new PsrLogAdapter($this->logger),
MessageFormatter::SHORT_FORMAT
);

$httpClient->addSubscriber($logPlugin);
}
}

if ($this->getSessionToken() instanceof SessionToken) {
$sessionTokenPlugin = new SessionTokenPlugin($this->getSessionToken());
$httpClient->addSubscriber($sessionTokenPlugin);
Expand Down Expand Up @@ -437,7 +488,7 @@ public function getCacheEnabled()
/**
* Set cache path
*
* You could simply pass an empty string to let the sys_get_temp_dir be used
* Leaving the second argument out will use sys_get_temp_dir()
*
* @param boolean $enabled
* @param string $path
Expand All @@ -464,4 +515,62 @@ public function getCachePath()
{
return $this->cachePath;
}

/**
* @param \Psr\Log\LoggerInterface $logger
* @return $this
*/
public function setLogger($logger)
{
$this->logger = $logger;

return $this;
}

/**
* @return \Psr\Log\LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}

/**
* @return boolean
*/
public function getLogEnabled()
{
return $this->logEnabled;
}

/**
* Set log path
*
* Leaving the second argument out will use sys_get_temp_dir()
*
* @param boolean $enabled
* @param string $path
* @return $this
*/
public function setLogging($enabled = true, $path = null)
{
$this->logEnabled = $enabled;
$this->logPath = (null === $path) ?
sys_get_temp_dir() . '/php-tmdb-api.log' :
$path
;

// @todo doesn't cover a custom client, would require un-registering all known plugins
$this->constructHttpClient();

return $this;
}

/**
* @return string
*/
public function getLogPath()
{
return $this->logPath;
}
}
20 changes: 20 additions & 0 deletions test/Tmdb/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ public function shouldAddCachePluginWhenEnabled()
));
}

/**
* @test
*/
public function shouldAddLoggingPluginWhenEnabled()
{
$token = new \Tmdb\ApiToken(self::API_TOKEN);
$client = new \Tmdb\Client($token);
$client->setLogging(true, '/tmp/php-tmdb-api.log');

$listeners = $client->getHttpClient()
->getClient()
->getEventDispatcher()
->getListeners();

$this->assertEquals(true, $this->isListenerRegistered(
$listeners,
'Guzzle\Plugin\Log\LogPlugin'
));
}

/**
* Find an plugin in an listeners array
*
Expand Down

0 comments on commit 857fde8

Please sign in to comment.