use Psr\Log\LoggerInterface; class MyService { private $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function doSomething() { // Do something... $this->logger->info('Something was done.'); } }
use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new Logger('my_logger'); $logger->pushHandler(new StreamHandler('path/to/your.log', Logger::INFO)); $service = new MyService($logger); $service->doSomething();In this example, we are using the Monolog library as our logging backend. We create a new instance of the `Logger` class and add a `StreamHandler` to write log messages to a file. We then create an instance of our `MyService` class and pass in the logger object. When we call the `doSomething()` method on our service, a log message will be written to the specified file. Package Library: The package library in this case is the PSR-Log package, which defines the LoggerInterface and other related interfaces for logging in PHP applications.