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 SpecificationsThe 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 informationEach 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:
- /**
- * Replace placeholders in the record information with contextual information
- */
- function interpolate ($message, array $context = Array ())
- {
- Constructs a substitution array for the key names that the curly braces contain
- $replace = Array ();
- foreach ($context as $key = = $val) {
- $replace [' {'. $key. '} '] = $val;
- }
- Replaces the placeholder in the record information, and finally returns the modified record information.
- Return Strtr ($message, $replace);
- }
- Contains record information with curly brace placeholders.
- $message = "User {username} created";
- The context array with the 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);
Copy Code1.3 ContextEach 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 interfacesThe 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
-
- namespace Psr\log;
- /**
- * Log Record instance
- *
- * Log information variable--message, * * must be a string or an object that implements the __tostring () method.
- *
- * * * * * * * * * can contain placeholders for formats such as "{foo}" (for foo) in the log information variable.
- * It will be replaced by a key value named "Foo" in the context array.
- *
- * Context array can carry arbitrary data, the only limitation is that when it carries a 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 ());
- /**
- * * * must take immediate action
- *
- * For example, if the entire site is collapsed, the database is unavailable, or otherwise, * * 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 situation
- *
- * For example, the program component is unavailable or an unexpected exception occurs.
- *
- * @param string $message
- * @param array $context
- * @return NULL
- */
- Public function Critical ($message, array $context = array ());
- /**
- * Run-time errors, do not need to take immediate action, but must be recorded for testing.
- *
- * @param string $message
- * @param array $context
- * @return NULL
- */
- Public Function error ($message, array $context = array ());
- /**
- * A non-error exception occurred.
- *
- * Example: Use of deprecated APIs, incorrect use of APIs, or unintended unnecessary errors.
- *
- * @param string $message
- * @param array $context
- * @return NULL
- */
- Public Function Warning ($message, array $context = array ());
- /**
- * General and 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 ());
- /**
- * Logging at any level
- *
- * @param mixed $level
- * @param string $message
- * @param array $context
- * @return NULL
- */
- Public function log ($level, $message, array $context = array ());
- }
Copy CodePsr\log\loggerawareinterface
-
- namespace Psr\log;
- /**
- * Logger-aware Definition Instance
- */
- Interface Loggerawareinterface
- {
- /**
- * Set up a log record instance
- *
- * @param loggerinterface $logger
- * @return NULL
- */
- Public Function Setlogger (Loggerinterface $logger);
- }
Copy CodePsr\log\loglevel
-
- 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 ';
- }
Copy CodeTurn from GitHub (Pizzaliu) |