Skip to content

Commit

Permalink
Adding a log adapter for Symfony - which caused interface change
Browse files Browse the repository at this point in the history
The LoggerInterface now uses the same methods as the Symfony HttpKernel
LoggerInterface.  This just means instead of a singular 'log' method, there
are several methods that can be used to log a message. (eg. debug, crit,
emerg, warn, notice, etc)
  • Loading branch information
justinrainbow committed Jul 23, 2012
1 parent 7cb7150 commit 91c06ad
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 28 deletions.
84 changes: 84 additions & 0 deletions src/Presque/Log/AbstractLogAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Log;

/**
* Abstract log adapter for Presque
*/
abstract class AbstractLogAdapter implements LoggerInterface
{
protected $log;

/**
* {@inheritDoc}
*/
public function emerg($message, array $context = array())
{
$this->log->emerg($message, $context);
}

/**
* {@inheritDoc}
*/
public function alert($message, array $context = array())
{
$this->log->alert($message, $context);
}

/**
* {@inheritDoc}
*/
public function crit($message, array $context = array())
{
$this->log->crit($message, $context);
}

/**
* {@inheritDoc}
*/
public function err($message, array $context = array())
{
$this->log->err($message, $context);
}

/**
* {@inheritDoc}
*/
public function warn($message, array $context = array())
{
$this->log->warn($message, $context);
}

/**
* {@inheritDoc}
*/
public function notice($message, array $context = array())
{
$this->log->notice($message, $context);
}

/**
* {@inheritDoc}
*/
public function info($message, array $context = array())
{
$this->log->info($message, $context);
}

/**
* {@inheritDoc}
*/
public function debug($message, array $context = array())
{
$this->log->debug($message, $context);
}
}
43 changes: 42 additions & 1 deletion src/Presque/Log/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,48 @@

namespace Presque\Log;

/**
* Simple log adapter interface
*/
interface LoggerInterface
{
function log($message, $priority = LOG_INFO, $extra = null);
/**
* @api
*/
function emerg($message, array $context = array());

/**
* @api
*/
function alert($message, array $context = array());

/**
* @api
*/
function crit($message, array $context = array());

/**
* @api
*/
function err($message, array $context = array());

/**
* @api
*/
function warn($message, array $context = array());

/**
* @api
*/
function notice($message, array $context = array());

/**
* @api
*/
function info($message, array $context = array());

/**
* @api
*/
function debug($message, array $context = array());
}
23 changes: 1 addition & 22 deletions src/Presque/Log/MonologLogAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,15 @@
/**
* Log adapter for Monolog
*
* @link https://github.com/guzzle/guzzle/blob/master/src/Guzzle/Common/Log/MonologLogAdapter.php
* @link https://github.com/Seldaek/monolog
*/
class MonologLogAdapter implements LoggerInterface
class MonologLogAdapter extends AbstractLogAdapter
{
/**
* syslog to Monolog mappings
*/
private static $mapping = array(
LOG_DEBUG => Logger::DEBUG,
LOG_INFO => Logger::INFO,
LOG_WARNING => Logger::WARNING,
LOG_ERR => Logger::ERROR,
LOG_CRIT => Logger::CRITICAL,
LOG_ALERT => Logger::ALERT
);

/**
* {@inheritdoc}
*/
public function __construct(Logger $logObject)
{
$this->log = $logObject;
}

/**
* {@inheritdoc}
*/
public function log($message, $priority = LOG_INFO, $extras = null)
{
$this->log->addRecord(self::$mapping[$priority], $message);
}
}
28 changes: 28 additions & 0 deletions src/Presque/Log/SymfonyLogAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Log;

use Symfony\Component\HttpKernel\Log\LoggerInterface as SymfonyLoggerInterface;

/**
* Symfony HttpKernel LoggerInterface adapter
*/
class SymfonyLogAdapter extends AbstractLogAdapter
{
/**
* {@inheritdoc}
*/
public function __construct(SymfonyLoggerInterface $logObject)
{
$this->log = $logObject;
}
}
5 changes: 1 addition & 4 deletions src/Presque/Worker/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ protected function process(QueueInterface $queue)
}

if ($this->hasLogger()) {
$this->logger->log(
sprintf('Starting job "%s"', $job),
LOG_DEBUG
);
$this->logger->debug(sprintf('Starting job "%s"', $job));
}

$job->prepare();
Expand Down
2 changes: 1 addition & 1 deletion tests/Presque/Tests/Worker/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testLoggingJobActivity()
$worker->setLogger($logger);

$logger
->shouldReceive('log')->once()->with('Starting job "Presque\Tests\Jobs\SimpleJob"', LOG_DEBUG);
->shouldReceive('debug')->once()->with('Starting job "Presque\Tests\Jobs\SimpleJob"');

$queue
->shouldReceive('reserve')->once()->andReturn($job);
Expand Down

0 comments on commit 91c06ad

Please sign in to comment.