Top Related Projects
Sends your logs to files, sockets, inboxes, databases and various web services
Quick Overview
The php-fig/log repository is a PHP Standards Recommendation (PSR) for logging interfaces. It defines a common interface for logging libraries, allowing for interoperability between different logging implementations in PHP applications. This standard is part of the PHP Framework Interop Group (PHP-FIG) efforts to improve collaboration across PHP projects.
Pros
- Promotes standardization and interoperability among logging libraries
- Simplifies switching between different logging implementations
- Encourages consistent logging practices across PHP projects
- Widely adopted by popular PHP frameworks and libraries
Cons
- Limited to basic logging functionality, may not cover all advanced use cases
- Requires adoption by both library developers and application developers for full benefit
- May introduce an additional layer of abstraction in some cases
- Some existing logging libraries may require adaptation to conform to the standard
Code Examples
- Basic logging example:
use Psr\Log\LoggerInterface;
class MyClass
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function doSomething()
{
$this->logger->info('Doing something');
// ... do something
}
}
- Using different log levels:
$logger->emergency('System is down');
$logger->alert('Database connection failed');
$logger->critical('Unexpected error occurred');
$logger->error('Unable to process request');
$logger->warning('Deprecated function used');
$logger->notice('User logged in');
$logger->info('Processing started');
$logger->debug('Variable value: ' . $value);
- Logging with context:
$logger->error('User {username} not found', ['username' => 'john_doe']);
Getting Started
To use PSR-3 logging in your project:
-
Install a PSR-3 compatible logger (e.g., Monolog):
composer require monolog/monolog
-
Create a logger instance:
use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new Logger('my_logger'); $logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
-
Use the logger in your code:
$logger->info('Hello, PSR-3 logging!');
Competitor Comparisons
Sends your logs to files, sockets, inboxes, databases and various web services
Pros of Monolog
- Comprehensive logging solution with numerous handlers and formatters
- Extensive documentation and active community support
- Flexible configuration options for various logging scenarios
Cons of Monolog
- Larger codebase and potential overhead for simple logging needs
- Steeper learning curve due to its extensive feature set
Code Comparison
Log (PSR-3 interface):
interface LoggerInterface
{
public function log($level, $message, array $context = array());
}
Monolog implementation:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
$log->warning('Foo', ['bar' => 'baz']);
Summary
Log provides a standardized interface for logging libraries, while Monolog is a full-featured logging implementation. Log focuses on defining a common API, whereas Monolog offers a rich set of tools for various logging needs. Monolog implements the PSR-3 interface defined by Log, ensuring compatibility with other PSR-3 compliant systems.
While Monolog provides more functionality out of the box, it may be overkill for simple projects. Log's interface is ideal for creating custom, lightweight logging solutions or for projects that require a standardized logging approach across different libraries.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PSR Log
This repository holds all interfaces/classes/traits related to PSR-3.
Note that this is not a logger of its own. It is merely an interface that describes a logger. See the specification for more details.
Installation
composer require psr/log
Usage
If you need a logger, you can use the interface like 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
}
}
You can then pick one of the implementations of the interface to get a logger.
If you want to implement the interface, you can require this package and
implement Psr\Log\LoggerInterface
in your code. Please read the
specification text
for details.
Top Related Projects
Sends your logs to files, sockets, inboxes, databases and various web services
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot