Skip to content

Commit

Permalink
Adding a generic log adapter system like Guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent 5c2ffca commit e2a0f10
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 15 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"predis/predis" : "0.7.*",
"symfony/event-dispatcher": "2.1.*@dev"
},
"require-dev": {
"monolog/monolog": "1.*"
},
"autoload": {
"psr-0": {
"": "src/"
Expand Down
9 changes: 7 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Presque;
namespace Presque\Event;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -29,13 +29,6 @@ interface EventDispatcherAwareInterface
*/
function setEventDispatcher(EventDispatcherInterface $eventDispatcher = null);

/**
* Returns an instance of the EventDispatcher
*
* @return EventDispatcherInterface
*/
function getEventDispatcher();

/**
* Checks if there is an EventDispatcher available to the current object.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Presque/Log/LoggerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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;

interface LoggerAwareInterface
{
/**
* Sets an instance of the LoggerInterface for this object. If the `$logger`
* is null, the object must remove any current LoggerInterface assigned to it.
*
* @param LoggerInterface $logger
*/
function setLogger(LoggerInterface $logger = null);
}
17 changes: 17 additions & 0 deletions src/Presque/Log/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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;

interface LoggerInterface
{
function log($message, $priority = LOG_INFO, $extra = null);
}
51 changes: 51 additions & 0 deletions src/Presque/Log/MonologLogAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 Monolog\Logger;

/**
* 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
{
/**
* 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);
}
}
Empty file.
46 changes: 41 additions & 5 deletions src/Presque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,101 @@ public function __construct($id = null)
$this->id = $id;
}

/**
* {@inheritDoc}
*/
public function getId()
{
return $this->id;
}

/**
* {@inheritDoc}
*/
public function addQueue(QueueInterface $queue)
{
$this->queues[] = $queue;
}

/**
* {@inheritDoc}
*/
public function removeQueue(QueueInterface $queue)
{
if ($key = array_search($queue, $this->queues)) {
unset($this->queues[$queue]);
}
}

/**
* {@inheritDoc}
*/
public function getQueues()
{
return $this->queues;
}

/**
* {@inheritDoc}
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher = null)
{
$this->eventDispatcher = $eventDispatcher;
}

public function getEventDispatcher()
{
return $this->eventDispatcher;
}

/**
* {@inheritDoc}
*/
public function hasEventDispatcher()
{
return null !== $this->eventDispatcher;
}

/**
* {@inheritDoc}
*/
public function setStatus($status)
{
$this->status = $status;
}

/**
* {@inheritDoc}
*/
public function getStatus()
{
return $this->status;
}

/**
* {@inheritDoc}
*/
public function isRunning()
{
return $this->getStatus() === StatusInterface::RUNNING;
}

/**
* {@inheritDoc}
*/
public function isDying()
{
return $this->getStatus() === StatusInterface::DYING;
}

/**
* {@inheritDoc}
*/
public function start()
{
$this->setStatus(StatusInterface::RUNNING);

$this->run();
}

/**
* {@inheritDoc}
*/
public function run()
{
while ($this->isRunning()) {
Expand All @@ -101,6 +132,11 @@ public function run()
}
}

/**
* Checks and executes a new Job for the given `$queue`
*
* @param QueueInterface $queue
*/
protected function process(QueueInterface $queue)
{
$job = $queue->reserve();
Expand Down
2 changes: 2 additions & 0 deletions src/Presque/WorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Presque;

use Presque\Event\EventDispatcherAwareInterface;

interface WorkerInterface extends EventDispatcherAwareInterface
{
function getId();
Expand Down

0 comments on commit e2a0f10

Please sign in to comment.