Phalcon 日誌記錄(Logging)

Phalcon提供了一個日誌記錄組件即 Phalcon\Logger。 咱們可使用此組件輸出日誌到不一樣的流中,如文件,系統日誌等。 這個組件還提供了其它的功能如日誌事務(相似於數據庫的事務), 配置選項, 還能夠輸出不一樣的格式,另外還支持多種過濾器。 Phalcon\Logger

提供了多種日誌記錄方式,從調試程序到跟蹤應用的執行以知足應用的需求。

適配器(Adapters)

此組件使用不一樣的流適配器來保存日信息。 咱們能夠按需使用適配器。支持的適配器以下:php

適配器 描述 接口
File 保存日誌到普通文件 Phalcon\Logger\Adapter\File
Stream 保存日誌到PHP流 Phalcon\Logger\Adapter\Stream
Syslog 保存到系統日誌 Phalcon\Logger\Adapter\Syslog
Firephp 發送日誌到FirePHP Phalcon\Logger\Adapter\FirePHP

建立日誌(Creating a Log)

下面的例子展現瞭如何建立日誌對象及如何添加日誌信息:html

<?php

use Phalcon\Logger\Adapter\File as FileAdapter; $logger = new FileAdapter("app/logs/test.log"); $logger->log("This is a message"); $logger->log("This is an error", \Phalcon\Logger::ERROR); $logger->error("This is another error"); 

產生的日誌信息以下:數據庫

[Tue, 17 Apr 12 22:09:02 -0500][DEBUG] This is a message
[Tue, 17 Apr 12 22:09:02 -0500][ERROR] This is an error [Tue, 17 Apr 12 22:09:02 -0500][ERROR] This is another error 

事務(Transactions)

保存日誌到適配器如文件(文件系統)是很是消耗系統資源的。 爲了減小應用性能上的開銷,咱們可使用日誌事務。 事務會把日誌記錄臨時的保存到內存中而後再 寫入到適配中(此例子中爲文件),(這個操做是個原子操做)api

<?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(); 

使用多個處理程序進行日誌記錄(Logging to Multiple Handlers)

Phalcon\Logger 也能夠同時保存日誌信息到多個適配器中:app

<?php

use Phalcon\Logger, Phalcon\Logger\Multiple as MultipleStream, Phalcon\Logger\Adapter\File as FileAdapter, 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"); 

信息發送的順序和處理器(適配器)註冊的順序相同。ide

信息格式(Message Formatting)

此組件使用 formatters 在信息發送前格式化日誌信息。 支持下然後格式:性能

適配器 描述 接口
Line 文本方式格式化信息 Phalcon\Logger\Formatter\Line
Json 使用JSON格式格式化信息 Phalcon\Logger\Formatter\Json
Syslog 使用系統提供的格式格式化信息 Phalcon\Logger\Formatter\Syslog

行格式化處理(Line Formatter)

使用單行格式格式化信息。 默認的格式以下:ui

[%date%][%type%] %message%spa

咱們可使用setFormat()來設置自定義格式。 下面是格式變量:操作系統

下面的例子中展現瞭如何修改日誌格式:

<?php

use Phalcon\Logger\Formatter\Line as LineFormatter; // 修改日誌格式 $formatter = new LineFormatter("%date% - %message%"); $logger->setFormatter($formatter); 

自定義格式處理(Implementing your own formatters)

若要實現自定義的格式則要實現 Phalcon\Logger\FormatterInterface 接口, 這樣才能擴展已有的格式或建立自定義的格式

適配器(Adapters)

下面的例子中展現了每種適配器的簡單用法:

數據流日誌記錄器(Stream Logger)

系統日誌保存消息到一個已註冊的有效的PHP流中。 這裏列出了可用的流: here <http://php.net/manual/en/wrappers.php>`_:

<?php

use Phalcon\Logger\Adapter\Stream as StreamAdapter; // 使用zlib壓縮流 $logger = new StreamAdapter("compress.zlib://week.log.gz"); // 發送消息到stderr $logger = new StreamAdapter("php://stderr"); 

文件日誌記錄器(File Logger)

文件適配器保存全部的日誌信息到普通的文件中。 默認狀況下日誌文件使用添加模式打開,打開文件後文件的指針會指向文件的尾端。 若是文件不存在,則會嘗試建立。 咱們能夠經過傳遞附加參數的形式來修改打開的模式:

<?php

use Phalcon\Logger\Adapter\File as FileAdapter; // 使用寫模式打開 $logger = new FileAdapter("app/logs/test.log", array( 'mode' => 'w' )); 

Syslog 日誌記錄器(Syslog Logger)

使用系統日誌適配器。 因爲操做系統的不一樣獲得的日誌也不盡相同:

<?php
use Phalcon\Logger\Adapter\Syslog as SyslogAdapter; // 基本用法 $logger = new SyslogAdapter(null); // Setting ident/mode/facility 參數設置 $logger = new SyslogAdapter("ident-name", array( 'option' => LOG_NDELAY, 'facility' => LOG_MAIL )); 

FirePHP 日誌記錄器(FirePHP Logger)

發送消息到FirePHP:

<?php

use Phalcon\Logger\Adapter\Firephp as Firephp; $logger = new Firephp(""); $logger->log("This is a message"); $logger->log("This is an error", \Phalcon\Logger::ERROR); $logger->error("This is another error"); 

自定義適配器(Implementing your own adapters)

若是開發者想自定義新的日誌組件則需實現此接口: Phalcon\Logger\AdapterInterface 。

相關文章
相關標籤/搜索