log README

The project's README from github.com/php-fig/log. As one markdown file: README.md.

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.

log (php-fig/log)

Searching Database…

10.4K ·GitHub
What is this?This is the PSR-3 standard package: a set of interfaces, classes and traits (centered on Psr\Log\LoggerInterface) that describe what a PHP logger should look like, so different libraries can share a common logging contract.
Is this a logger I can use directly, or just a specification?It is purely a specification — no actual logging engine, file writer, or handlers are included; you still need a real logger that implements the interface.
How do I actually use this in a project?Type-hint against Psr\Log\LoggerInterface in your own code, then plug in any logger that implements it; if you're building your own logger, require this package and implement the interface yourself.
Do I need to write a logger from scratch to use this?No — you only need a PSR-3-compatible logging library (such as Monolog) that already implements the interface; this package just defines the contract you code against.
Does this package write logs on its own?No, it has no runtime logging behavior at all — it's strictly interfaces, classes and traits describing the contract, with the real work left to whichever implementation you choose.
What's included besides the LoggerInterface?The package also ships supporting classes and traits tied to the PSR-3 spec, giving a consistent base for implementing the standard log-level methods (debug, info, error, etc.).
Why would I use this instead of just picking a logging library?PSR-3 is a shared contract: coding against Psr\Log\LoggerInterface lets your code work with any compliant logger (like Monolog) without locking you into one implementation.
Is there a reference implementation bundled in?No, it is intentionally implementation-free — you bring your own logger class that implements Psr\Log\LoggerInterface, as described in the full specification.
Creator
php-fig
Size
0.1 MB
Stars
10.4K
Age
13y
Commits · 90 Days