Elgg  Version 3.0
Loggable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
12 trait Loggable {
13 
17  private $logger;
18 
26  public function setLogger(LoggerInterface $logger = null) {
27  $this->logger = $logger;
28  }
29 
34  public function getLogger() {
35 
36  if ($this->logger) {
37  return $this->logger;
38  } else if (Application::$_instance) {
39  return Application::$_instance->_services->logger;
40  }
41 
42  // Application hasn't been bootstrapped
43  return Logger::factory();
44  }
45 
55  public function log($level, $message, array $context = []) {
56  if ($message instanceof \Throwable) {
57  if (!isset($message->timestamp)) {
58  $message->timestamp = time();
59  }
60 
61  $context['exception'] = $message;
62  }
63 
64  // PSR interface is void, but Monolog returns a boolean
65  $logged = $this->getLogger()->log($level, $message, $context);
66  return isset($logged) ? $logged : true;
67  }
68 
69 }
$context
Definition: add.php:8
static $_instance
Reference to the loaded Application.
Definition: Application.php:75
static factory(InputInterface $input=null, OutputInterface $output=null)
Build a new logger.
Definition: Logger.php:82
getLogger()
Returns logger.
Definition: Loggable.php:34
Configuration exception.
setLogger(LoggerInterface $logger=null)
Set (or remove) the logger.
Definition: Loggable.php:26
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:55