Logger interface

Last update: 2023-05-16
  • Created for:
  • Experienced

To start working with a logger, you must create an instance of \Psr\Log\LoggerInterface. With this interface, you can call the following functions to write data to log files:

One way to do that is explained in the Log database activity example.

Another way follows:

class SomeModel
     private $logger;

     public function __construct(\Psr\Log\LoggerInterface $logger)
         $this->logger = $logger;

     public function doSomething()
         try {
             //do something
         } catch (\Exception $e) {
             $this->logger->critical('Error message', ['exception' => $e]);

The preceding example shows that SomeModel receives a \Psr\Log\LoggerInterface object using constructor injection. In a method doSomething, if some error occurred, it is logged to a method critical ($this->logger->critical($e);).

RFC 5424 defines eight log levels (debug, info, notice, warning, error, critical, alert, and emergency).

On this page