Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/PsrTarget.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace samdark\log;

use Closure;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use yii\base\InvalidConfigException;
Expand All @@ -17,7 +17,12 @@
*/
class PsrTarget extends Target implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* The logger instance.
*
* @var LoggerInterface|Closure|null
*/
public $logger;

/**
* @var bool If enabled, logger use original timestamp from buffer
Expand Down Expand Up @@ -60,7 +65,17 @@ public function __construct($config = [])
$this->_levels = $this->_levelMap;
parent::__construct($config);
}


/**
* Sets a logger.
*
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* @return LoggerInterface
* @throws InvalidConfigException
Expand All @@ -70,6 +85,11 @@ public function getLogger()
if ($this->logger === null) {
throw new InvalidConfigException('Logger should be configured with Psr\Log\LoggerInterface.');
}
if ($this->logger instanceof Closure) {
$loggerClosure = $this->logger;
$this->logger = $loggerClosure();
}

return $this->logger;
}

Expand Down