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
这是什么?这是 PSR-3 标准包:一组接口、类和 trait(以 Psr\Log\LoggerInterface 为核心),用于描述 PHP 日志器应有的样子,使不同的库能够共享统一的日志约定。
这个我能直接用来记日志吗,还是只是一份规范?它纯粹是一份规范,不包含实际的日志引擎、文件写入器或处理器;你仍然需要一个实现了该接口的真正日志器。
在项目里具体怎么用?在你自己的代码中以 Psr\Log\LoggerInterface 作为类型提示,然后接入任何实现了该接口的日志器;如果你要自己开发日志器,就引入这个包并自行实现该接口。
用它之前,我需要从零写一个日志器吗?不需要。你只需要一个兼容 PSR-3 的日志库(例如 Monolog),它已经实现了该接口;这个包只是定义你编写代码所依据的约定。
这个包自己会写日志吗?不会,它完全没有运行时的日志功能。它只是描述约定的接口、类和 trait,实际工作交由你选择的具体实现来完成。
除了 LoggerInterface,还包含什么?该包还附带了与 PSR-3 规范相关的辅助类和 trait,为实现标准的日志级别方法(debug、info、error 等)提供统一的基础。
为什么要用它,而不直接选一个日志库?PSR-3 是一份共享约定。针对 Psr\Log\LoggerInterface 编写代码,就能让你的代码与任何符合标准的日志器(如 Monolog)配合使用,而不会被绑定到某一个具体实现上。
包里有自带的参考实现吗?没有,它有意不包含任何实现。你需要自己编写一个实现 Psr\Log\LoggerInterface 的日志器类,具体细节见完整规范。
Creator
php-fig
Size
0.1 MB
Stars
10.4K
Age
13y
Commits · 90 Days