PHP PSR-3 log interface specification

WBOY
Release: 2016-07-23 08:54:41
Original
962 people have browsed it
Log interface specification

This article formulates the general interface specification of the log library.

The main purpose of this specification is to allow the logging library to record log information in a simple and universal way by receiving a PsrLogLoggerInterface object.
If necessary, the framework and CMS content management system can extend this interface, but they must follow this specification. This can ensure that the log interface can still be connected normally when using third-party class library files.

Keywords "must" ("MUST"), "must not/must not" ("MUST NOT"), "need" ("REQUIRED"),

"will" ("SHALL"), "won't" ("SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"),
"recommended" ("RECOMMENDED"), "can" ("MAY") and "optional" ("OPTIONAL") is described in detail in [RFC 2119][].

The implementer in this article refers to the class library or framework that implements the LoggerInterface interface. In turn, they are the users of LoggerInterface.

Specifications

1.1 Basic specifications
The LoggerInterface interface defines eight external methods, which are used to record the eight levels of logs defined in [RFC 5424][]: debug, info, notice, warning, error, critical, alert and emergency.

The ninth method - log, its first parameter is the level of the record. This method can be called using a predefined level constant as a parameter, which must have the same effect as directly calling the above eight methods. If the incoming level constant parameter is not predefined, an exception of type PsrLogInvalidArgumentException must be thrown. Users

should not call this method using unsupported level constants under uncertain circumstances. 1.2 Record information Each of the above methods accepts a string type or an object with __toString() method as the record information parameter, so that the implementer can treat it as a string, otherwise the implementer mustconvert it by themselves into a string.

The record information parameters

can carry placeholders, and the implementer 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 placeholder name

must be enclosed by an opening curly bracket { and a closing bracket }. But there must be no spaces between the curly braces and the name.

The name of the placeholder

should only consist of A-Z, a-z, 0-9, underscore _, and English period ., other characters are reserved for future placeholder specifications. Implementers can

generate the final log by applying different escaping and conversion strategies for placeholders.

And the user should not escape the placeholder in advance without knowing the context.

The following is an example of placeholder usage:


/** * Replace placeholders in record information with contextual information

*/

function interpolate($message, array $context = array())

{
// Construct a curly bracket enclosed Replacement array of key names
$replace = array();
    foreach ($context as $key => $val) {
  1. $replace['{' . $key . '}'] = $val;
  2. }
  3. //Replace the placeholders in the record information, and finally return the modified record information.
  4. return strtr($message, $replace);
  5. }
  6. // Contains record information with curly bracket placeholders.
  7. $message = "User {username} created";
  8. // Context array with replacement information, the key name is the placeholder name, and the key value is the replacement value.
  9. $context = array('username' => 'bolivar');
  10. // Output "Username bolivar created"
  11. echo interpolate($message, $context);
  12. Copy code
  13. 1.3 Context

    Each recording function accepts a context array parameter to load information that cannot be represented by the string type. It can load any information, so the implementer must ensure that it can correctly handle the information it loads. For the data it loads, must not throw exceptions, or generate PHP errors, warnings or reminder messages (error, warning , notice).

    If an Exception object is passed in through the context parameter, must use 'exception' as the key name. Logging exception information is very common, so if it can be implemented at the bottom of the logging class library, it will allow the implementer to get rid of the exception information.
    Of course, when using it, the implementer must
    ensure whether 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 PsrLogAbstractLogger class makes it easy to implement the LoggerInterface interface by simply inheriting it and implementing the log method, and the other eight methods can pass logging information and context information to it.

    Similarly, to use PsrLogLoggerTrait, you only need to implement the log method. However, it is important to note that LoggerInterface needs to be implemented before the traits reusable code block can implement the interface.

    When there is no available logger, the PsrLogNullLogger interface

    can

    provide users with a backup log "black hole". However, when context construction is very resource-intensive, logging with conditional checks may be a better approach. The PsrLogLoggerAwareInterface interface only includes a

    setLogger(LoggerInterface $logger) method, which the framework can use to automatically connect to any logging instance.


    The reusable code block of the PsrLogLoggerAwareTrait trait can be used in any class, and the equivalent interface can be easily implemented through the $this->logger it provides.

    The PsrLogLogLevel class loads eight logging level constants.

    Bag

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

    PsrLogLoggerInterface


    1. namespace PsrLog;
    2. /**
    3. * Logging instance
    4. *
    5. * Log information variable - message, **must** be a string or an object that implements the __toString() method.
    6. *
    7. * The log information variable **can** contain a placeholder in the format "{foo}" (representing foo),
    8. * which will be replaced by the key value "foo" in the context array.
    9. *
    10. * The context array can carry any data. The only restriction is that when it carries an exception object, its key name must be "exception".
    11. *
    12. * For details, please refer to: https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-3-logger-interface-cn.md
    13. */
    14. interface LoggerInterface
    15. {
    16. /**
    17. * The system is unavailable
    18. *
    19. * @param string $message
    20. * @param array $context
    21. * @return null
    22. */
    23. public function emergency($message, array $context = array());
    24. /**
    25. * **Must** take action immediately
    26. *
    27. * For example: in the event that the entire website is down, the database is unavailable, or other circumstances, an alert text message should be sent to wake you up.
    28. *
    29. * @param string $message
    30. * @param array $context
    31. * @return null
    32. */
    33. public function alert($message, array $context = array());
    34. /**
    35. * Emergency
    36. *
    37. * For example: program components are unavailable or unexpected exceptions occur.
    38. *
    39. * @param string $message
    40. * @param array $context
    41. * @return null
    42. */
    43. public function critical($message, array $context = array());
    44. /**
    45. * Errors that occur during runtime do not require immediate action, but must be recorded for detection.
    46. *
    47. * @param string $message
    48. * @param array $context
    49. * @return null
    50. */
    51. public function error($message, array $context = array());
    52. /**
    53. * A non-error exception occurred.
    54. *
    55. * For example: using a deprecated API, using an API incorrectly or causing unexpected and unnecessary errors.
    56. *
    57. * @param string $message
    58. * @param array $context
    59. * @return null
    60. */
    61. public function warning($message, array $context = array());
    62. /**
    63. *Generally important events.
    64. *
    65. * @param string $message
    66. * @param array $context
    67. * @return null
    68. */
    69. public function notice($message, array $context = array());
    70. /**
    71. * Important events
    72. *
    73. * For example: user login and SQL records.
    74. *
    75. * @param string $message
    76. * @param array $context
    77. * @return null
    78. */
    79. public function info($message, array $context = array());
    80. /**
    81. * debug details
    82. *
    83. * @param string $message
    84. * @param array $context
    85. * @return null
    86. */
    87. public function debug($message, array $context = array());
    88. /**
    89. * Logging at any level
    90. *
    91. * @param mixed $level
    92. * @param string $message
    93. * @param array $context
    94. * @return null
    95. */
    96. public function log($level, $message, array $context = array());
    97. }
    复制代码

    PsrLogLoggerAwareInterface


    1. namespace PsrLog;
    2. /**
    3. * logger-aware definition instance
    4. */
    5. interface LoggerAwareInterface
    6. {
    7. /**
    8. * Set up a logging instance
    9. *
    10. * @param LoggerInterface $logger
    11. * @return null
    12. */
    13. public function setLogger(LoggerInterface $logger);
    14. }
    复制代码

    PsrLogLogLevel


    1. namespace PsrLog;
    2. /**
    3. * Log level constant definition
    4. */
    5. class LogLevel
    6. {
    7. const EMERGENCY = 'emergency';
    8. const ALERT = 'alert';
    9. const CRITICAL = 'critical';
    10. const ERROR = 'error';
    11. const WARNING = 'warning';
    12. const NOTICE = 'notice';
    13. const INFO = 'info';
    14. const DEBUG = 'debug';
    15. }
    复制代码

    转自Github(PizzaLiu)

PHP, PSR


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template