123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
-
- namespace Illuminate\Log;
-
- use Closure;
- use RuntimeException;
- use Psr\Log\LoggerInterface;
- use Illuminate\Log\Events\MessageLogged;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Contracts\Events\Dispatcher;
- use Illuminate\Contracts\Support\Arrayable;
-
- class Logger implements LoggerInterface
- {
- /**
- * The underlying logger implementation.
- *
- * @var \Psr\Log\LoggerInterface
- */
- protected $logger;
-
- /**
- * The event dispatcher instance.
- *
- * @var \Illuminate\Contracts\Events\Dispatcher|null
- */
- protected $dispatcher;
-
- /**
- * Create a new log writer instance.
- *
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher
- * @return void
- */
- public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
- {
- $this->logger = $logger;
- $this->dispatcher = $dispatcher;
- }
-
- /**
- * Log an emergency message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function emergency($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log an alert message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function alert($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log a critical message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function critical($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log an error message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function error($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log a warning message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function warning($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log a notice to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function notice($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log an informational message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function info($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log a debug message to the logs.
- *
- * @param string $message
- * @param array $context
- * @return void
- */
- public function debug($message, array $context = [])
- {
- $this->writeLog(__FUNCTION__, $message, $context);
- }
-
- /**
- * Log a message to the logs.
- *
- * @param string $level
- * @param string $message
- * @param array $context
- * @return void
- */
- public function log($level, $message, array $context = [])
- {
- $this->writeLog($level, $message, $context);
- }
-
- /**
- * Dynamically pass log calls into the writer.
- *
- * @param string $level
- * @param string $message
- * @param array $context
- * @return void
- */
- public function write($level, $message, array $context = [])
- {
- $this->writeLog($level, $message, $context);
- }
-
- /**
- * Write a message to the log.
- *
- * @param string $level
- * @param string $message
- * @param array $context
- * @return void
- */
- protected function writeLog($level, $message, $context)
- {
- $this->fireLogEvent($level, $message = $this->formatMessage($message), $context);
-
- $this->logger->{$level}($message, $context);
- }
-
- /**
- * Register a new callback handler for when a log event is triggered.
- *
- * @param \Closure $callback
- * @return void
- *
- * @throws \RuntimeException
- */
- public function listen(Closure $callback)
- {
- if (! isset($this->dispatcher)) {
- throw new RuntimeException('Events dispatcher has not been set.');
- }
-
- $this->dispatcher->listen(MessageLogged::class, $callback);
- }
-
- /**
- * Fires a log event.
- *
- * @param string $level
- * @param string $message
- * @param array $context
- * @return void
- */
- protected function fireLogEvent($level, $message, array $context = [])
- {
- // If the event dispatcher is set, we will pass along the parameters to the
- // log listeners. These are useful for building profilers or other tools
- // that aggregate all of the log messages for a given "request" cycle.
- if (isset($this->dispatcher)) {
- $this->dispatcher->dispatch(new MessageLogged($level, $message, $context));
- }
- }
-
- /**
- * Format the parameters for the logger.
- *
- * @param mixed $message
- * @return mixed
- */
- protected function formatMessage($message)
- {
- if (is_array($message)) {
- return var_export($message, true);
- } elseif ($message instanceof Jsonable) {
- return $message->toJson();
- } elseif ($message instanceof Arrayable) {
- return var_export($message->toArray(), true);
- }
-
- return $message;
- }
-
- /**
- * Get the underlying logger implementation.
- *
- * @return \Psr\Log\LoggerInterface
- */
- public function getLogger()
- {
- return $this->logger;
- }
-
- /**
- * Get the event dispatcher instance.
- *
- * @return \Illuminate\Contracts\Events\Dispatcher
- */
- public function getEventDispatcher()
- {
- return $this->dispatcher;
- }
-
- /**
- * Set the event dispatcher instance.
- *
- * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
- * @return void
- */
- public function setEventDispatcher(Dispatcher $dispatcher)
- {
- $this->dispatcher = $dispatcher;
- }
-
- /**
- * Dynamically proxy method calls to the underlying logger.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->logger->{$method}(...$parameters);
- }
- }
|