PHP规范PSR3(日志接口)介绍

本文档描述了用于记录库的通用接口。

主要目标是允许库以简单和通用的方式接收Psr \ Log \ LoggerInterface对象并将日志写入其中。具有自定义需求的框架和CMS可以扩展接口以用于它们自己的目的,但是应该保持与该文档的兼容性。这可确保应用程序使用的第三方库可以写入集中式应用程序日志。

本文档中的关键词“必须”,“必须”,“必需”,“应该”,“不应该”,“应该”,“不应该”,“推荐”,“可以”和“可选”按照RFC 2119中的描述进行解释。

本文档中的单词实现者将被解释为在日志相关的库或框架中实现LoggerInterface的人。记录器的用户称为用户。

1 规格

1.1 基础知识

  • LoggerInterface公开了八种方法来将日志写入八个RFC 5424级别(调试,信息,通知,警告,错误,严重,警报,紧急情况)。
  • 第九种方法log接受日志级别作为第一个参数。使用其中一个日志级别常量调用此方法必须与调用特定级别的方法具有相同的结果。如果实现不知道该级别,则调用具有此规范未定义的级别的此方法必须抛出Psr \ Log \ InvalidArgumentException。用户不应该在不知道当前实现是否支持的情况下使用自定义级别。

1.2 消息

  • 每个方法都接受一个字符串作为消息,或一个带有__toString()方法的对象。实现者可以对传递的对象进行特殊处理。如果不是这样,实现者必须将其转换为字符串。
  • 消息可以包含占位符,实现者可以替换上下文数组中的值。
  • 占位符名称必须对应于上下文数组中的键。
  • 占位符名称必须用一个左括号{和一个右括号}分隔。分隔符和占位符名称之间不能有任何空格。
  • 占位符名称应该仅由字符A-Z,a-z,0-9,下划线_和句点组成。其他字符的使用保留用于将来修改占位符规范。
  • 实现者可以使用占位符来实现各种转义策略并转换日志以供显示。用户不应预先转义占位符值,因为他们无法知道数据将在哪个上下文中显示。

以下是占位符插值的示例实现,仅供参考:

<?php

/**
 * Interpolates context values into the message placeholders.
 */
function interpolate($message, array $context = array())
{
    // build a replacement array with braces around the context keys
    $replace = array();
    foreach ($context as $key => $val) {
        // check that the value can be casted to string
        if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
            $replace['{' . $key . '}'] = $val;
        }
    }

    // interpolate replacement values into the message and return
    return strtr($message, $replace);
}

// a message with brace-delimited placeholder names
$message = "User {username} created";

// a context array of placeholder names => replacement values
$context = array('username' => 'bolivar');

// echoes "User bolivar created"
echo interpolate($message, $context);

1.3 上下文

每个方法都接受一个数组作为上下文数据这意味着保存任何不适合字符串的无关信息。该数组可以包含任何内容。实现者必须确保他们尽可能地对待上下文数据。上下文中的给定值绝不能抛出异常,也不会引发任何php错误,警告或通知。

如果在上下文数据中传递了Exception对象,则它必须位于“exception”键中。记录异常是一种常见模式,这允许实现者在日志后端支持时从异常中提取堆栈跟踪。在使用它之前,实现者仍然必须验证'exception'键实际上是一个Exception,因为它可能包含任何东西。

1.4 助手类和接口

Psr \ Log \ AbstractLogger类使您可以通过扩展它并实现通用日志方法来非常轻松地实现LoggerInterface。其他八种方法是将消息和上下文转发给它。

同样,使用Psr \ Log \ LoggerTrait只需要实现通用日志方法。请注意,由于traits无法实现接口,因此在这种情况下仍需要实现LoggerInterface。

Psr \ Log \ NullLogger与接口一起提供。如果没有为它们提供记录器,接口的用户可以使用它来提供后备“黑洞”实现。但是,如果上下文数据创建很昂贵,则条件记录可能是更好的方法。

Psr \ Log \ LoggerAwareInterface只包含一个setLogger(LoggerInterface $ logger)方法,框架可以使用该方法使用记录器自动连接任意实例。

Psr \ Log \ LoggerAwareTrait特性可用于在任何类中轻松实现等效接口。它允许您访问$ this-> logger。

Psr \ Log \ LogLevel类保存八个日志级别的常量。

2 包

描述的接口和类以及相关的异常类和用于验证实现的测试套件是作为psr / log包的一部分提供的。

3 Psr\Log\LoggerInterface

<?php

namespace Psr\Log;

/**
 * Describes a logger instance
 *
 * The message MUST be a string or object implementing __toString().
 *
 * The message MAY contain placeholders in the form: {foo} where foo
 * will be replaced by the context data in key "foo".
 *
 * The context array can contain arbitrary data, the only assumption that
 * can be made by implementors is that if an Exception instance is given
 * to produce a stack trace, it MUST be in a key named "exception".
 *
 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
 * for the full interface specification.
 */
interface LoggerInterface
{
    /**
     * System is unusable.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function emergency($message, array $context = array());

    /**
     * Action must be taken immediately.
     *
     * Example: Entire website down, database unavailable, etc. This should
     * trigger the SMS alerts and wake you up.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function alert($message, array $context = array());

    /**
     * Critical conditions.
     *
     * Example: Application component unavailable, unexpected exception.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function critical($message, array $context = array());

    /**
     * Runtime errors that do not require immediate action but should typically
     * be logged and monitored.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function error($message, array $context = array());

    /**
     * Exceptional occurrences that are not errors.
     *
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
     * that are not necessarily wrong.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function warning($message, array $context = array());

    /**
     * Normal but significant events.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function notice($message, array $context = array());

    /**
     * Interesting events.
     *
     * Example: User logs in, SQL logs.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function info($message, array $context = array());

    /**
     * Detailed debug information.
     *
     * @param string $message
     * @param array $context
     * @return void
     */
    public function debug($message, array $context = array());

    /**
     * Logs with an arbitrary level.
     *
     * @param mixed $level
     * @param string $message
     * @param array $context
     * @return void
     */
    public function log($level, $message, array $context = array());
}

4. Psr\Log\LoggerAwareInterface

<?php

namespace Psr\Log;

/**
 * Describes a logger-aware instance
 */
interface LoggerAwareInterface
{
    /**
     * Sets a logger instance on the object
     *
     * @param LoggerInterface $logger
     * @return void
     */
    public function setLogger(LoggerInterface $logger);
}

5. Psr\Log\LogLevel

<?php

namespace Psr\Log;

/**
 * Describes log levels
 */
class LogLevel
{
    const EMERGENCY = 'emergency';
    const ALERT     = 'alert';
    const CRITICAL  = 'critical';
    const ERROR     = 'error';
    const WARNING   = 'warning';
    const NOTICE    = 'notice';
    const INFO      = 'info';
    const DEBUG     = 'debug';
}

 

猜你喜欢

转载自blog.csdn.net/u013702678/article/details/83450901