use Monolog\Logger; use Monolog\Handler\StreamHandler; $log = new Logger('emergency'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::EMERGENCY)); $log->emergency('Database connection lost!');
use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; class MyService { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function doSomething() { try { // Do something... } catch (Exception $e) { $this->logger->log(LogLevel::EMERGENCY, 'Something went terribly wrong!', ['exception' => $e]); } } }In this example, we define a class that depends on a LoggerInterface instance and use it to log an emergency message in case of an exception. We pass the LogLevel::EMERGENCY constant to the log() method and include the exception object in the context array. The package library used in these examples is Monolog, a popular logging library that implements the PSR-3 interface.