Elgg  Version master
Loggable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits;
4 
6 use Elgg\Logger;
7 use Psr\Log\LoggerInterface;
8 
14 trait Loggable {
15 
19  private $logger;
20 
28  public function setLogger(?LoggerInterface $logger = null) {
29  $this->logger = $logger;
30  }
31 
37  public function getLogger() {
38 
39  if ($this->logger) {
40  return $this->logger;
41  } else if (Application::$_instance) {
42  return Application::$_instance->internal_services->logger;
43  }
44 
45  // Application hasn't been bootstrapped
46  return Logger::factory();
47  }
48 
58  public function log($level, $message, array $context = []) {
59  if ($message instanceof \Throwable) {
60  $context['exception'] = $message;
61  }
62 
63  // PSR interface is void, but Monolog returns a boolean
64  $logged = $this->getLogger()->log($level, $message, $context);
65  return $logged ?? true;
66  }
67 
76  public function logDeprecatedMessage(string $message, string $version): void {
77  $this->getLogger()->warning("Deprecated in {$version}: {$message}");
78  }
79 }
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.
Definition: Loggable.php:76
getLogger()
Returns logger.
Definition: Loggable.php:37
setLogger(?LoggerInterface $logger=null)
Set (or remove) the logger.
Definition: Loggable.php:28
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
$context
Definition: add.php:8
static $_instance
Reference to the loaded Application.
Definition: Application.php:71
static factory(?InputInterface $input=null, ?OutputInterface $output=null)
Build a new logger.
Definition: Logger.php:78
$version