PHP PSR-3 Log Interface Specification

Source: Internet
Author: User
Tags constant definition php error rfc
Log Interface Specification

In this paper, the general interface specification of Log class library is developed.

The main purpose of this specification is to allow the Log class library to record log information in a simple and generic manner by receiving a Psr\log\loggerinterface object.
The framework and CMS Content Management system can be extended to this interface, if required, subject to this specification,
This ensures that the log interface is still properly docked when using a third-party class library file.

Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),
"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),
A detailed description of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in [RFC 2119][].

The implementation of this article refers to the implementation of the Loggerinterface interface of the class library or framework, in turn, they are loggerinterface users.

Specification description

1.1 Basic Specifications

The Loggerinterface interface defines eight methods for recording eight levels of logs defined in [RFC 5424][]: Debug, info, notice, warning, error, critical, alert, and emerge Ncy.

The Nineth method--log, whose first parameter is the rank of the record. This method can be called using a predefined rank constant as a parameter and must have the same effect as calling the eight methods directly. If an incoming rank constant parameter is not predefined, you must throw an exception of type psr\log\invalidargumentexception. In the case of uncertainty, the user should not use an unsupported level constant to invoke this method.

1.2 Record information

Each of these methods accepts a string type or an object with the __tostring () method as the record information parameter, so that the implementing person can treat it as a string, otherwise the implementation must convert it to a string by itself.

The record information parameter can carry a placeholder, and the implementing person can replace the other values with the corresponding context.

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

The name of the placeholder must be enclosed by a left curly brace {and a closing parenthesis}. However, there must be no whitespace between curly braces and names.

The name of the placeholder should only be A-Z, a-z,0-9, Underline _, and a period in English. The other characters are reserved for future placeholder specifications.

The implementation can generate the final log by using a different escape and conversion strategy for the placeholder.
The user should not escape the placeholder in advance without knowing the context.

The following is an example of a placeholder use:

    1. /**
    2. * Replace placeholders in the record information with contextual information
    3. */
    4. function interpolate ($message, array $context = Array ())
    5. {
    6. Constructs a substitution array for the key names that the curly braces contain
    7. $replace = Array ();
    8. foreach ($context as $key = = $val) {
    9. $replace [' {'. $key. '} '] = $val;
    10. }
    11. Replaces the placeholder in the record information, and finally returns the modified record information.
    12. Return Strtr ($message, $replace);
    13. }
    14. Contains record information with curly brace placeholders.
    15. $message = "User {username} created";
    16. The context array with the replacement information, the key name is the placeholder name, and the key value is the replacement value.
    17. $context = Array (' username ' = ' bolivar ');
    18. Output "Username Bolivar created"
    19. Echo interpolate ($message, $context);
Copy Code1.3 Context

Each record function accepts a context array parameter that is used to load information that cannot be represented by a string type. It can load any information, so the implementation must ensure that the information it loads is properly handled, that it must not throw an exception for the data it is loading, or that a PHP error, warning, or alert message (Error, warning, NOTICE).

If you want to pass in a Exception object through a context parameter, you must use ' Exception ' as the key name.
It is very common to log exception information, so if it can be implemented at the bottom of the Record class library, it will allow the cobwebs to be removed from the exception information.
Of course, when using it, the implementation must ensure that the key named ' Exception ' is really a 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 contextual information to it.

Similarly, it is only possible to implement the Log method with psr\log\loggertrait. However, it is important to note that the implement Loggerinterface is also required before traits can be used to implement the interface of the reusable code block.

In the absence of a logger available, the Psr\log\nulllogger interface can provide an alternate log "black hole" for the consumer. However, logging with conditional checking may be a better approach when the build of the context is very resource-intensive.

The Psr\log\loggerawareinterface interface includes only one
The Setlogger (Loggerinterface $logger) method enables the framework to use it to automate the connection of arbitrary log record instances.

PSR\LOG\LOGGERAWARETRAIT Trait reusable code block can be used in any class, simply through the $this it provides->logger, you can easily implement the equivalent interface.

The Psr\log\loglevel class loads eight record-level constants.

Package

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

Psr\log\loggerinterface

  1. namespace Psr\log;
  2. /**
  3. * Log Record instance
  4. *
  5. * Log information variable--message, * * must be a string or an object that implements the __tostring () method.
  6. *
  7. * * * * * * * * * can contain placeholders for formats such as "{foo}" (for foo) in the log information variable.
  8. * It will be replaced by a key value named "Foo" in the context array.
  9. *
  10. * Context array can carry arbitrary data, the only limitation is that when it carries a 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. * System not available
  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 immediate action
  26. *
  27. * For example, if the entire site is collapsed, the database is unavailable, or otherwise, * * Should send an alert message 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 situation
  36. *
  37. * For example, the program component is unavailable or an unexpected exception occurs.
  38. *
  39. * @param string $message
  40. * @param array $context
  41. * @return NULL
  42. */
  43. Public function Critical ($message, array $context = array ());
  44. /**
  45. * Run-time errors, do not need to take immediate action, but must be recorded for testing.
  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. * Example: Use of deprecated APIs, incorrect use of APIs, or unintended 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. * General and 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 record.
  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. }
Copy Code

Psr\log\loggerawareinterface

    1. namespace Psr\log;
    2. /**
    3. * Logger-aware Definition Instance
    4. */
    5. Interface Loggerawareinterface
    6. {
    7. /**
    8. * Set up a log record instance
    9. *
    10. * @param loggerinterface $logger
    11. * @return NULL
    12. */
    13. Public Function Setlogger (Loggerinterface $logger);
    14. }
Copy Code

Psr\log\loglevel

    1. namespace Psr\log;
    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. }
Copy Code

Turn from GitHub (Pizzaliu)

PHP, PSR
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.