Specification for PHPPSR-3 log interfaces

Source: Internet
Author: User
Tags rfc
Specification for PHPPSR-3 log interfaces
Log interface specification

This article develops a general interface specification for the log class library.

The main purpose of this specification is to allow the Log class library to record the Log information in a simple and common way by receiving a Psr \ Log \ LoggerInterface object.
If necessary,YesThis API is extended, but this specification must be followed,
This ensures that the log interface can still be normally connected when a third-party class library file is used.

Key words: "MUST" ("MUST"), "must not/must not" ("must not"), "need" ("REQUIRED "),
"Will" ("SHALL"), "will NOT" ("shall not"), "SHOULD" ("shocould"), "shouldn't" ("shocould NOT "),
For detailed descriptions of "recommendations" ("RECOMMENDED"), "Yes" ("MAY"), and "OPTIONAL" ("OPTIONAL"), see [RFC 2119] [].

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

Specifications

1.1 Basic Specifications

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

The ninth method, log. The first parameter is the record level. You can use a predefined level constant as a parameter to call this method,RequiredIt has the same effect as directly calling the above eight methods. If the passed level constant parameter is not pre-definedRequiredThrow an exception of the type of SRS \ Log \ InvalidArgumentException. In the case of uncertaintyShouldn'tThis method is called using unsupported level constants.

1.2 Record Information

Each of the above methods accepts a string type or an object with the _ toString () method as the record information parameter, so that the implementer can treat it as a string, otherwise the implementerRequiredConvert it into a string.

Record Information parametersYesCarry placeholders, implementersYesReplace other values with corresponding values according to the context.

PlaceholdersRequiredIt must be consistent with the key name in the context array.

Placeholder nameRequiredIncluded by a left curly braces {and a right bracket. But between curly braces and namesCertainly notThere is a space character.

Placeholder nameShouldOnly A-Z, a-z, 0-9, underscore _, and English periods. Other characters are reserved as future placeholder specifications.

ImplementerYesUse different conversion and conversion policies for placeholders to generate the final log.
However, if the user does not know the following,Shouldn'tEscape placeholders in advance.

The following is an example of using a placeholder:

  1. /**
  2. * Replace the placeholder in the record information with context information
  3. */
  4. Function interpolate ($ message, array $ context = array ())
  5. {
  6. // Construct a replacement array of the key names contained in curly brackets
  7. $ Replace = array ();
  8. Foreach ($ context as $ key => $ val ){
  9. $ Replace ['{'. $ key. '}'] = $ val;
  10. }
  11. // Replace the placeholder in the record information and return the modified record information.
  12. Return strtr ($ message, $ replace );
  13. }
  14. // Record information containing placeholders with curly braces.
  15. $ Message = "User {username} created ";
  16. // Context array with 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 );

1.3 Context

Each record function accepts a context array parameter to load information that cannot be expressed by the string type. ItYesLoad any information, so the implementerRequiredMake sure that you can correctly process the information it loads. for the data it loads,Certainly notThrows an exception or generates PHP errors, warnings, or reminders (error, warning, or notice ).

If you need to pass in an Exception object through context parameters,RequiredUse 'exception' as the key name.
Recording exception information is common, so if it can be implemented at the underlying layer of the record library, it will allow the real-time users to get rid of the exception information.
Of course, when the implementer uses it,RequiredMake sure that the key value 'exception' is an exception.YesLoad any information.

1.4 helper classes and interfaces

This makes it easy to implement the LoggerInterface by inheriting it and implementing the Log method, the other eight methods can pass the record information and context information to it.

Similarly, you only need to implement the Log method by using the DSRs \ log \ LoggerTrait. However, it should be noted that implement LoggerInterface is also required before traits reusable code blocks cannot implement interfaces.

When no Log recorder is available, the SRS \ Log \ NullLogger interfaceYesProvides users with a backup log "black hole ". However, when context building consumes a lot of resources, it may be better to record logs with conditional checks.

Only one of the following interfaces is available:
SetLogger (LoggerInterface $ logger) method. the framework can use it to automatically connect to any log record instance.

You can use this reusable code block in any class by using the $ this-> logger provided by this code block to easily implement the same interface.

The Psr \ Log \ LogLevel class contains eight record level constants.

Package

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

Psr \ Log \ LoggerInterface

  1. Namespace Psr \ Log;
  2. /**
  3. * Logging instance
  4. *
  5. * The log information variable -- message, ** must ** be a string or an object that implements the _ toString () method.
  6. *
  7. * The log information variable ** can ** contain placeholders in the format of "{foo}" (representing foo,
  8. * It will be replaced by the key value named "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, see: 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. * ** Required ** take immediate action
  26. *
  27. * For example, if the entire website crashes, the database is unavailable, or in other cases, ** you should ** send an alert text 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
  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. * If an error occurs during running, you do not need to take immediate action, but you must record it 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. * An error is returned.
  54. *
  55. * For example, an abandoned API is used, an API is incorrectly used, or an unexpected unnecessary error occurs.
  56. *
  57. * @ Param string $ message
  58. * @ Param array $ context
  59. * @ Return null
  60. */
  61. Public function warning ($ message, array $ context = array ());
  62. /**
  63. * General 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 logon 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. * Log records of 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. }

Psr \ Log \ LoggerAwareInterface

  1. Namespace Psr \ Log;
  2. /**
  3. * Logger-aware defines an instance
  4. */
  5. Interface LoggerAwareInterface
  6. {
  7. /**
  8. * Set a logging instance
  9. *
  10. * @ Param LoggerInterface $ logger
  11. * @ Return null
  12. */
  13. Public function setLogger (LoggerInterface $ logger );
  14. }

Psr \ Log \ LogLevel

  1. Namespace Psr \ Log;
  2. /**
  3. * Log level constant definition
  4. */
  5. Class LogLevel
  6. {
  7. Const EMERGENCY = 'ergency ';
  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. }

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.