Skip to content

Commit

Permalink
Allow use of \Plop\Psr3Logger as a singleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoirotte committed Oct 3, 2014
1 parent d3b1dca commit b9604f6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Psr3Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@
*/
class Psr3Logger extends \Psr\Log\AbstractLogger
{
protected $factory = null;
static $factory = null;
static $instance = null;

public function __construct()
{
if (static::$factory === null) {
static::$factory = new \Plop\RecordFactory(
new \Plop\Interpolator\Psr3()
);
}
}

public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}

public function log($level, $message, array $context = array())
{
Expand All @@ -44,16 +62,11 @@ public function log($level, $message, array $context = array())
* - register_tick_function()
* - debug_backtrace()
*/

if ($this->factory === null) {
$this->factory = new \Plop\RecordFactory(new \Plop\Interpolator\Psr3());
}

$logging = \Plop\Plop::getInstance();
$factory = $logging->getRecordFactory();
try {
// Switch to a factory that uses PSR3-interpolation.
$logging->setRecordFactory($this->factory);
$logging->setRecordFactory(static::$factory);
$logging->$level($message, $context);
} catch (Exception $e) {}

Expand Down

0 comments on commit b9604f6

Please sign in to comment.