Skip to content

Instantly share code, notes, and snippets.

@chameleonbr
Last active August 29, 2015 13:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chameleonbr/9865542 to your computer and use it in GitHub Desktop.
Redis Logger with predis pipeline and rpush or publish
<?php
use Phalcon\Logger\Exception;
/**
* Logger class using predis with rpush, publish and pipeline
*/
class Redis extends \Phalcon\Logger\Adapter implements \Phalcon\DI\InjectionAwareInterface
{
/**
* Name
*/
protected $name;
protected $formatter;
/**
* Adapter options
*/
protected $inTransaction = false;
// todo: change transactionLog to SplQueue
protected $transactionLog = array();
protected $service;
protected $method = '';
/**
* Class constructor.
*
* @param string $name
* @param array $options
* @throws \Phalcon\Logger\Exception
*/
public function __construct($di,$options = array())
{
$default = array('name' => 'log','formatter' => null,'service' => 'redis','method'=>'rpush');
$options = array_merge($default,$options);
$this->name = $options['name'];
$this->formatter = $options['formatter'];
$this->service = $options['service'];
$this->method = $options['method'];
$this->setDI($di);
}
public function getFormatter()
{
if(!($this->formatter instanceof \Phalcon\Logger\FormatterInterface)){
$this->formatter = new \Phalcon\Logger\Formatter\Json();
}
return $this->formatter;
}
public function setFormatter($formatter)
{
$this->formatter = $formatter;
return $this;
}
public function getDI()
{
return $this->di;
}
public function setDI($di)
{
$this->di = $di;
return $this;
}
public function begin()
{
$this->inTransaction = true;
}
public function rollback()
{
if($this->inTransaction){
$this->transactionLog = array();
$this->inTransaction = false;
return true;
}
return false;
}
public function commit()
{
if($this->inTransaction){
if(!empty($this->transactionLog)){
$log = &$this->transactionLog;
$formatter = $this->getFormatter();
$method = &$this->method;
$name = &$this->name;
$this->getDI()->{$this->service}->pipeline(function($pipe) use ($log,$formatter,$method,$name){
foreach($log as $line){
$str = call_user_func_array(array(&$formatter,'format'),$line);
$pipe->$method($name, $str);
}
});
}
$this->inTransaction = false;
$this->transactionLog = array();
}
}
public function logInternal($message, $type, $time, $context = array())
{
if(!$this->inTransaction){
$formatter = $this->getFormatter();
$di = $this->getDI();
$method = &$this->method;
$name = &$this->name;
$line = $formatter->format($message, $type, $time, $context);
$di[$this->service]->{$method}($name, $line);
}else{
array_push($this->transactionLog, array($message, $type, $time, $context));
}
}
public function close()
{
}
}
@chameleonbr
Copy link
Author

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Wrong number of parameters' in /var/www/myapp/app/Logger/Redis.php on line 113
( ! ) BadMethodCallException: Wrong number of parameters in /var/www/myapp/app/library/Receitapr/Logger/Redis.php on line 113
Call Stack

Time Memory Function Location

6 0.0020 325064 MyApp\Logger\Redis->logInternal( ) ../test.php:0
7 0.0021 325272 format ( ) ../Redis.php:113

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