<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 日志 `Phalcon\Logger` 是一個組件,其目的是為應用程序提供日志記錄服務。它使用不同的適配器提供對不同后端的日志記錄。它還提供事務日志記錄,配置選項,不同格式和過濾器。您可以將 `Phalcon\Logger` 用于應用程序所需的每個日志記錄,從調試過程到跟蹤應用程序流。 ## 適配器 該組件使用適配器來存儲記錄的消息。適配器的使用允許一個通用的日志記錄接口,它提供了在必要時輕松切換后端的能力。支持的適配器是: | 適配器 | 描述 | | ----------------------------------- | ------------------------- | | `Phalcon\Logger\Adapter\File` | 記錄到純文本文件 | | `Phalcon\Logger\Adapter\Stream` | 記錄到 PHP 流 | | `Phalcon\Logger\Adapter\Syslog` | 記錄到系統日志 | | `Phalcon\Logger\Adapter\FirePHP` | 記錄到FirePHP | ### 工廠 使用適配器選項加載Logger Adapter類 ```php <?php use Phalcon\Logger\Factory; $options = [ 'name' => 'log.txt', 'adapter' => 'file', ]; $logger = Factory::load($options); ``` ## 創建日志 下面的示例顯示了如何創建日志并向其添加消息: ```php <?php use Phalcon\Logger; use Phalcon\Logger\Adapter\File as FileAdapter; $logger = new FileAdapter('app/logs/test.log'); // 這些是可用的不同日志級別: $logger->critical( 'This is a critical message' ); $logger->emergency( 'This is an emergency message' ); $logger->debug( 'This is a debug message' ); $logger->error( 'This is an error message' ); $logger->info( 'This is an info message' ); $logger->notice( 'This is a notice message' ); $logger->warning( 'This is a warning message' ); $logger->alert( 'This is an alert message' ); // 您還可以將 log() 方法與Logger常量一起使用: $logger->log( 'This is another error message', Logger::ERROR ); // 如果沒有給出常數,則假設為DEBUG。 $logger->log( 'This is a message' ); // 您也可以像這樣傳遞上下文參數 $logger->log( 'This is a {message}', [ 'message' => 'parameter' ] ); ``` 生成的日志如下: ```bash [Tue, 28 Jul 15 22:09:02 -0500][CRITICAL] This is a critical message [Tue, 28 Jul 15 22:09:02 -0500][EMERGENCY] This is an emergency message [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a debug message [Tue, 28 Jul 15 22:09:02 -0500][ERROR] This is an error message [Tue, 28 Jul 15 22:09:02 -0500][INFO] This is an info message [Tue, 28 Jul 15 22:09:02 -0500][NOTICE] This is a notice message [Tue, 28 Jul 15 22:09:02 -0500][WARNING] This is a warning message [Tue, 28 Jul 15 22:09:02 -0500][ALERT] This is an alert message [Tue, 28 Jul 15 22:09:02 -0500][ERROR] This is another error message [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a message [Tue, 28 Jul 15 22:09:02 -0500][DEBUG] This is a parameter ``` 您還可以使用 `setLogLevel()` 方法設置日志級別。此方法采用Logger常量,僅保存與常量一樣重要或更重要的日志消息: ```php <?php use Phalcon\Logger; use Phalcon\Logger\Adapter\File as FileAdapter; $logger = new FileAdapter('app/logs/test.log'); $logger->setLogLevel( Logger::CRITICAL ); ``` 在上面的示例中,只有關鍵和緊急消息才會保存到日志中。默認情況下,一切都已保存。 ## 事務 將數據記錄到適配器,即文件(文件系統)在性能方面總是昂貴的操作。要解決這個問題,您可以利用日志記錄事務。事務將日志數據臨時存儲在內存中,稍后在單個原子操作中將數據寫入相關適配器(在本例中為File)。 ```php <?php use Phalcon\Logger\Adapter\File as FileAdapter; // 創建日志記錄器 $logger = new FileAdapter('app/logs/test.log'); // 開始事務 $logger->begin(); // 添加消息 $logger->alert( 'This is an alert' ); $logger->error( 'This is another error' ); // 將消息提交到文件 $logger->commit(); ``` ## 記錄到多個處理程序 只需一次調用,`Phalcon\Logger` 就可以向多個處理程序發送消息: ```php <?php use Phalcon\Logger; use Phalcon\Logger\Multiple as MultipleStream; use Phalcon\Logger\Adapter\File as FileAdapter; use Phalcon\Logger\Adapter\Stream as StreamAdapter; $logger = new MultipleStream(); $logger->push( new FileAdapter('test.log') ); $logger->push( new StreamAdapter('php://stdout') ); $logger->log( 'This is a message' ); $logger->log( 'This is an error', Logger::ERROR ); $logger->error( 'This is another error' ); ``` 消息按照注冊順序發送給處理程序。 ## 消息格式格式化 此組件在將消息發送到后端之前使用格式化程序格式化消息。可用的格式化程序是: | 適配器 | 描述 | | ------------------------------------- | -------------------------------------------------------- | | `Phalcon\Logger\Formatter\Line` | 使用單行字符串格式化消息 | | `Phalcon\Logger\Formatter\Firephp` | 格式化消息,以便將它們發送到FirePHP| | `Phalcon\Logger\Formatter\Json` | 準備要使用JSON編碼的消息 | | `Phalcon\Logger\Formatter\Syslog` | 準備要發送到syslog的消息 | ### 單行格式化 使用單行字符串格式化消息。默認的日志記錄格式為: ```bash [%date%][%type%] %message% ``` 您可以使用`setFormat()`更改默認格式,這允許您通過定義自己的格式來更改已記錄消息的格式。允許的日志格式變量是: | 變量 | 描述 | | --------- | ---------------------------------------- | | %message% | 預計將記錄消息本身| | %date% | 日期添加到消息 | | %type% | 帶有消息類型的大寫字符串 | 以下示例顯示了如何更改日志格式: ```php <?php use Phalcon\Logger\Formatter\Line as LineFormatter; $formatter = new LineFormatter('%date% - %message%'); // 更改日志記錄器格式 $logger->setFormatter($formatter); ``` ### 實現自己的格式化程序 必須實現 `Phalcon\Logger\FormatterInterface` 接口才能創建自己的記錄器格式化程序或擴展現有的格式化程序。 ## 適配器 The following examples show the basic use of each adapter: ### Stream Logger 流記錄器將消息寫入PHP中的有效注冊流。[此處](http://php.net/manual/en/wrappers.php)提供了一個流列表: ```php <?php use Phalcon\Logger\Adapter\Stream as StreamAdapter; // 使用zlib壓縮打開流 $logger = new StreamAdapter('compress.zlib://week.log.gz'); // 將日志寫入stderr $logger = new StreamAdapter('php://stderr'); ``` ### 文件記錄器 此記錄器使用純文件記錄任何類型的數據。默認情況下,所有記錄器文件都使用追加模式打開,該模式打開文件僅供寫入;將文件指針放在文件的末尾。如果該文件不存在,將嘗試創建它。您可以通過將其他選項傳遞給構造函數來更改此模式: ```php <?php use Phalcon\Logger\Adapter\File as FileAdapter; // 以'w'模式創建文件記錄器 $logger = new FileAdapter( 'app/logs/test.log', [ 'mode' => 'w', ] ); ``` ### Syslog記錄器 此記錄器將消息發送到系統記錄器。系統日志行為可能因操作系統而異。 ```php <?php use Phalcon\Logger\Adapter\Syslog as SyslogAdapter; // 基本使用 $logger = new SyslogAdapter(null); // 設置 ident/mode/facility $logger = new SyslogAdapter( 'ident-name', [ 'option' => LOG_NDELAY, 'facility' => LOG_MAIL, ] ); ``` ### FirePHP記錄器 此記錄器在[FirePHP](http://www.firephp.org/)(Firefox的[FirePHP](http://www.firephp.org/)擴展)顯示的HTTP響應標頭中發送消息。 ```php <?php use Phalcon\Logger; use Phalcon\Logger\Adapter\Firephp as Firephp; $logger = new Firephp(''); $logger->log( 'This is a message' ); $logger->log( 'This is an error', Logger::ERROR ); $logger->error( 'This is another error' ); ``` ### 實現自己的適配器 必須實現 `Phalcon\Logger\AdapterInterface` 接口才能創建自己的記錄器適配器或擴展現有的記錄器適配器。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看