psr/log
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The PSR Log package offers a common interface for logging libraries in PHP. However, it's important to note that PSR/log is not a logger by itself, but it defines a standardized interface that describes how a logger should behave. This interoperability allows developers to build onto or interchangeably utilize various logging libraries that comply with the PSR-3 standard without changing their core code.
Using the PSR Log package in your PHP application is straightforward and involves a few steps. First, install the package into your project with Composer using the following command:
composer require psr/log
Then, use the Psr\Log\LoggerInterface in your class and inject it in the constructor, saving it as a private property. This logger can then be used within your class to log information and errors. Here's an example of how you can achieve this:
<?php
use Psr\Log\LoggerInterface;
class Foo
{
private $logger;
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}
public function doSomething()
{
if ($this->logger) {
$this->logger->info('Doing work');
}
try {
$this->doSomethingElse();
} catch (Exception $exception) {
$this->logger->error('Oh no!', array('exception' => $exception));
}
// do something useful
}
}
To get a logger, you can choose one of the implementations of the LoggerInterface.
The documentation for PSR/log can be found in the form of a specification text directly on the official PHP Standards Recommendation (PSR) GitHub page in the PSR-3 logger interface section. You can find it at this URL: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md. This contains all the details required to implement the PSR\Log\LoggerInterface correctly.