banker/src/LoggerTrait.php

58 lines
1.1 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-11-30 11:56:15 -05:00
* PHP version 8+
2016-09-06 17:03:43 -04:00
*
* @package Banker
* @author Timothy J. Warren <tim@timshomepage.net>
2023-03-16 13:09:36 -04:00
* @copyright 2016 - 2023 Timothy J. Warren
2016-09-06 17:03:43 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2023-03-16 13:09:36 -04:00
* @version 4.1.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
*/
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
*/
2023-03-16 14:24:39 -04:00
public function setLogger(LoggerInterface $logger): void
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);
}
}
}