PHP規範PSR3(日誌介面)介紹
本文件描述了用於記錄庫的通用介面。
主要目標是允許庫以簡單和通用的方式接收Psr \ Log \ LoggerInterface物件並將日誌寫入其中。具有自定義需求的框架和CMS可以擴充套件介面以用於它們自己的目的,但是應該保持與該文件的相容性。這可確保應用程式使用的第三方庫可以寫入集中式應用程式日誌。
本文件中的關鍵詞“必須”,“必須”,“必需”,“應該”,“不應該”,“應該”,“不應該”,“推薦”,“可以”和“可選”按照RFC 2119中的描述進行解釋。
本文件中的單詞實現者將被解釋為在日誌相關的庫或框架中實現LoggerInterface的人。記錄器的使用者稱為使用者。
1 規格
1.1 基礎知識
- LoggerInterface公開了八種方法來將日誌寫入八個RFC 5424級別(除錯,資訊,通知,警告,錯誤,嚴重,警報,緊急情況)。
- 第九種方法log接受日誌級別作為第一個引數。使用其中一個日誌級別常量呼叫此方法必須與呼叫特定級別的方法具有相同的結果。如果實現不知道該級別,則呼叫具有此規範未定義的級別的此方法必須丟擲Psr \ Log \ InvalidArgumentException。使用者不應該在不知道當前實現是否支援的情況下使用自定義級別。
1.2 訊息
- 每個方法都接受一個字串作為訊息,或一個帶有__toString()方法的物件。實現者可以對傳遞的物件進行特殊處理。如果不是這樣,實現者必須將其轉換為字串。
- 訊息可以包含佔位符,實現者可以替換上下文陣列中的值。
- 佔位符名稱必須對應於上下文陣列中的鍵。
- 佔位符名稱必須用一個左括號{和一個右括號}分隔。分隔符和佔位符名稱之間不能有任何空格。
- 佔位符名稱應該僅由字元A-Z,a-z,0-9,下劃線_和句點組成。其他字元的使用保留用於將來修改佔位符規範。
- 實現者可以使用佔位符來實現各種轉義策略並轉換日誌以供顯示。使用者不應預先轉義佔位符值,因為他們無法知道資料將在哪個上下文中顯示。
以下是佔位符插值的示例實現,僅供參考:
<?php /** * Interpolates context values into the message placeholders. */ function interpolate($message, array $context = array()) { // build a replacement array with braces around the context keys $replace = array(); foreach ($context as $key => $val) { // check that the value can be casted to string if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { $replace['{' . $key . '}'] = $val; } } // interpolate replacement values into the message and return return strtr($message, $replace); } // a message with brace-delimited placeholder names $message = "User {username} created"; // a context array of placeholder names => replacement values $context = array('username' => 'bolivar'); // echoes "User bolivar created" echo interpolate($message, $context);
1.3 上下文
每個方法都接受一個數組作為上下文資料這意味著儲存任何不適合字串的無關資訊。該陣列可以包含任何內容。實現者必須確保他們儘可能地對待上下文資料。上下文中的給定值絕不能丟擲異常,也不會引發任何php錯誤,警告或通知。
如果在上下文資料中傳遞了Exception物件,則它必須位於“exception”鍵中。記錄異常是一種常見模式,這允許實現者在日誌後端支援時從異常中提取堆疊跟蹤。在使用它之前,實現者仍然必須驗證'exception'鍵實際上是一個Exception,因為它可能包含任何東西。
1.4 助手類和介面
Psr \ Log \ AbstractLogger類使您可以通過擴充套件它並實現通用日誌方法來非常輕鬆地實現LoggerInterface。其他八種方法是將訊息和上下文轉發給它。
同樣,使用Psr \ Log \ LoggerTrait只需要實現通用日誌方法。請注意,由於traits無法實現介面,因此在這種情況下仍需要實現LoggerInterface。
Psr \ Log \ NullLogger與介面一起提供。如果沒有為它們提供記錄器,介面的使用者可以使用它來提供後備“黑洞”實現。但是,如果上下文資料建立很昂貴,則條件記錄可能是更好的方法。
Psr \ Log \ LoggerAwareInterface只包含一個setLogger(LoggerInterface $ logger)方法,框架可以使用該方法使用記錄器自動連線任意例項。
Psr \ Log \ LoggerAwareTrait特性可用於在任何類中輕鬆實現等效介面。它允許您訪問$ this-> logger。
Psr \ Log \ LogLevel類儲存八個日誌級別的常量。
2 包
描述的介面和類以及相關的異常類和用於驗證實現的測試套件是作為psr / log包的一部分提供的。
3 Psr\Log\LoggerInterface
<?php
namespace Psr\Log;
/**
* Describes a logger instance
*
* The message MUST be a string or object implementing __toString().
*
* The message MAY contain placeholders in the form: {foo} where foo
* will be replaced by the context data in key "foo".
*
* The context array can contain arbitrary data, the only assumption that
* can be made by implementors is that if an Exception instance is given
* to produce a stack trace, it MUST be in a key named "exception".
*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
* for the full interface specification.
*/
interface LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param array $context
* @return void
*/
public function emergency($message, array $context = array());
/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
* @return void
*/
public function alert($message, array $context = array());
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
* @return void
*/
public function critical($message, array $context = array());
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
* @return void
*/
public function error($message, array $context = array());
/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
* @return void
*/
public function warning($message, array $context = array());
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
* @return void
*/
public function notice($message, array $context = array());
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
* @return void
*/
public function info($message, array $context = array());
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
* @return void
*/
public function debug($message, array $context = array());
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array());
}
4. Psr\Log\LoggerAwareInterface
<?php
namespace Psr\Log;
/**
* Describes a logger-aware instance
*/
interface LoggerAwareInterface
{
/**
* Sets a logger instance on the object
*
* @param LoggerInterface $logger
* @return void
*/
public function setLogger(LoggerInterface $logger);
}
5. Psr\Log\LogLevel
<?php
namespace Psr\Log;
/**
* Describes log levels
*/
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';
}