banker/src/LoggerTrait.php

65 lines
1.2 KiB
PHP
Raw Normal View History

2016-10-19 09:57:06 -04:00
<?php declare(strict_types=1);
2016-09-06 17:03:43 -04:00
/**
* Banker
*
* A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
2016-09-06 17:03:43 -04:00
*
2021-02-05 17:05:56 -05:00
* PHP version 7.4+
2016-09-06 17:03:43 -04:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2021-02-05 17:05:56 -05:00
* @copyright 2016 - 2021 Timothy J. Warren
2016-09-06 17:03:43 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2021-02-05 17:05:56 -05:00
* @version 3.2.0
2016-09-06 17:03:43 -04:00
* @link https://git.timshomepage.net/timw4mail/banker
*/
namespace Aviat\Banker;
2016-10-19 09:57:06 -04:00
use Psr\Log\{
LoggerAwareTrait,
LoggerInterface,
LogLevel,
NullLogger
};
2016-09-06 17:03:43 -04:00
/**
* Trait for keeping track of logger objects
*/
trait LoggerTrait {
2016-10-19 09:57:06 -04:00
use LoggerAwareTrait;
2016-09-06 17:03:43 -04:00
/**
2016-10-19 09:57:06 -04:00
* Return the existing logger instance or
2016-09-06 17:03:43 -04:00
* a NullLogger, if no instance set
*
* @return LoggerInterface
*/
protected function getLogger(): LoggerInterface
2016-09-06 17:03:43 -04:00
{
if ($this->logger === NULL)
{
$this->logger = new NullLogger();
}
return $this->logger;
}
2016-10-19 09:57:06 -04:00
2016-09-06 17:03:43 -04:00
/**
* Set a logger to keep track of errors
*
* @param LoggerInterface $logger
* @return self
*/
public function setLogger(LoggerInterface $logger): self
2016-09-06 17:03:43 -04:00
{
$this->logger = $logger;
2016-10-19 09:57:06 -04:00
2016-09-06 17:03:43 -04:00
// Set the logger for the current driver too
if (isset($this->driver))
{
$this->driver->setLogger($logger);
}
2016-10-19 09:57:06 -04:00
2016-09-06 17:03:43 -04:00
return $this;
}
}