The general interface of psr/log log database

Keywords: Programming PHP github Database SQL

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md

composer require psr/log

Log interface specification

In this paper, the general interface specification of log class library is established.

The main purpose of this specification is to make the log class library record log information by receiving a Psr\Log\LoggerInterface object in a simple and general way.
If necessary, the framework and CMS content management system can extend this interface, but this specification shall be followed,
This can ensure that when using the third-party class library files, the log interface can still be docked normally.

The keywords MUST, MUST NOT, REQUIRED
"Will" ("SHALL"), "will not" ("SHALL NOT"), "SHOULD" ("shald"), "shouldn't" ("shald not")
For a detailed description of RECOMMENDED, MAY and OPTIONAL, see RFC 2119 .

The implementers in this article refer to the libraries or frameworks that implement the LoggerInterface interface. In turn, they are the users of LoggerInterface.

1. Specification

1.1 basic specifications

  • LoggerInterface defines eight methods for recording RFC 5424 Eight levels of logging defined in: debug, info, notice, warning, error, critical, alert, and emergency.

  • The ninth method, log, whose first parameter is the level of the record. This method can be called with a predefined level constant as a parameter, and must have the same effect as directly calling the above eight methods. If the passed in level constant parameter is not predefined, an exception of type Psr\Log\InvalidArgumentException must be thrown. In case of uncertainty, the consumer should not call this method with an unsupported level constant.

1.2 record information

  • Each of the above methods accepts a string type or an object with the \\\\\\\\\\\.

  • Record information parameters can carry placeholders, and implementers can replace others with corresponding values according to the context.

    Where the placeholder must be consistent with the key name in the context array.

    The name of the placeholder must be contained by a left curly bracket {and a right bracket}. But there must be no spaces between curly braces and names.

    The name of the placeholder should only consist of A-Z, a-z,0-9, underscore, and period. Other characters are reserved for future placeholder specifications.

    The implementer can generate the final log by using different escape and conversion strategies for placeholders.
    The user should not escape the placeholder in advance without knowing the context.

    Here is an example of using placeholders:

    /**
    * Replace placeholders in record information with context information
    */
    function interpolate($message, array $context = array())
    {
      // Construct an alternative array of key names contained in curly braces
      $replace = array();
      foreach ($context as $key => $val) {
          $replace['{' . $key . '}'] = $val;
      }
    
      // Replace the placeholder in the record information, and finally return the modified record information.
      return strtr($message, $replace);
    }
    
    // Record information with curly bracket placeholders.
    $message = "User {username} created";
    
    // The context array with replacement information. The key name is the placeholder name and the key value is the replacement value.
    $context = array('username' => 'bolivar');
    
    // Output "Username bolivar created"
    echo interpolate($message, $context);
    

1.3 context

  • Each record function takes a context array parameter to load information that the string type cannot represent. It can load any information, so the implementer must ensure that the loaded information can be handled correctly. For the loaded data, it must not throw an exception or generate PHP error, warning or reminder information (error, warning, notice).

  • To pass in an exception object through a context parameter, 'exception' must be used as the key name.
    It is very common to record exception information, so if it can be implemented in the bottom layer of the record class library, it can let the implementer get rid of the exception information.
    Of course, when implementers use it, they have to make sure that the key value with the key name 'Exception' is really an Exception, after all, it can load any information.

1.4 helper classes and interfaces

  • The Psr\Log\AbstractLogger class makes it easy to implement the LoggerInterface interface by simply inheriting it and implementing the log method, while the other eight methods can pass the record information and context information to it.

  • Similarly, you only need to implement the log method in PSR \ log \ loggertrade. However, it should be noted that the implementation loggerinterface is needed before the reusable code block of traits can implement the interface.

  • When no logger is available, the Psr\Log\NullLogger interface can provide a backup log "black hole" for users. However, conditional logging may be a better way when context building is very resource intensive.

  • Psr\Log\LoggerAwareInterface interface only includes one
    setLogger(LoggerInterface $logger) method, which can be used by the framework to automatically connect any logging instance.

  • PSR \ log \ loggeraware trait trace reusable code block can be used in any class. Only through the $this - > logger provided by it, you can easily implement the same interface.

  • The Psr\Log\LogLevel class loads eight record level constants.

2. packs

The above interfaces, classes, and related exception classes, as well as a series of implementation detection files, are included in the psr/log In the package.

3. Psr\Log\LoggerInterface

<?php

namespace Psr\Log;

/**
 * Logging instance
 *
 * Log information variable -- message, * * must * * be a string or an object that implements the \\\\\\\\.
 *
 * The log information variable * * can * * contain placeholders in the format of "{foo}" (for foo),
 * It will be replaced by a key named "foo" in the context array.
 *
 * The context array can carry any data. The only restriction is that when it carries an exception object, its key name must be "exception".
 *
 * For details, please refer to: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-3-logger-interface-cn.md
 */
interface LoggerInterface
{
    /**
     * System not available
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function emergency($message, array $context = array());

    /**
     * **Action must be taken immediately
     *
     * For example, in the event that the entire website is down, the database is unavailable, or other circumstances, * * should * * send an alert message to wake you up.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function alert($message, array $context = array());

    /**
     * Emergency
     *
     * For example, program components are not available or unexpected exceptions occur.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function critical($message, array $context = array());

    /**
     * Errors in operation do not require immediate action, but must be recorded for detection.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function error($message, array $context = array());

    /**
     * A non error exception occurred.
     *
     * For example, a deprecated API is used, an API is used incorrectly, or an unexpected unnecessary error.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function warning($message, array $context = array());

    /**
     * General important events.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function notice($message, array $context = array());

    /**
     * Important events
     *
     * For example: user login and SQL record.
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function info($message, array $context = array());

    /**
     * debug details
     *
     * @param string $message
     * @param array $context
     * @return null
     */
    public function debug($message, array $context = array());

    /**
     * Any level of logging
     *
     * @param mixed $level
     * @param string $message
     * @param array $context
     * @return null
     */
    public function log($level, $message, array $context = array());
}

4. Psr\Log\LoggerAwareInterface

<?php

namespace Psr\Log;

/**
 * logger-aware Defining examples
 */
interface LoggerAwareInterface
{
    /**
     * Set up a logging instance
     *
     * @param LoggerInterface $logger
     * @return null
     */
    public function setLogger(LoggerInterface $logger);
}

5. Psr\Log\LogLevel

 
<?php

namespace Psr\Log;

/**
 * Log level constant definition
 */
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';
}

Posted by mark_18 on Wed, 11 Mar 2020 23:05:21 -0700