# 日志記錄(Logging)
Phalcon提供了一個日志記錄組件即[Phalcon\\Logger](http://docs.iphalcon.cn/api/Phalcon_Logger.html)。 我們可以使用此組件輸出日志到不同的流中,如文件,系統日志等。 這個組件還提供了其它的功能如日志事務(類似于數據庫的事務), 配置選項, 還可以輸出不同的格式,另外還支持多種過濾器。[Phalcon\\Logger](http://docs.iphalcon.cn/api/Phalcon_Logger.html)提供了多種日志記錄方式,從調試程序到跟蹤應用的執行以滿足應用的需求。
## 適配器(Adapters)
此組件使用不同的流適配器來保存日信息。 我們可以按需使用適配器。支持的適配器如下:
| 適配器 | 描述 |
| --- | --- |
| [Phalcon\\Logger\\Adapter\\File](http://docs.iphalcon.cn/api/Phalcon_Logger_Adapter_File.html) | 保存日志到普通文件 |
| [Phalcon\\Logger\\Adapter\\Stream](http://docs.iphalcon.cn/api/Phalcon_Logger_Adapter_Stream.html) | 保存日志到PHP流 |
| [Phalcon\\Logger\\Adapter\\Syslog](http://docs.iphalcon.cn/api/Phalcon_Logger_Adapter_Syslog.html) | 保存到系統日志 |
| [Phalcon\\Logger\\Adapter\\FirePHP](http://docs.iphalcon.cn/api/Phalcon_Logger_Adapter_Firephp.html) | 發送日志到FirePHP |
## 創建日志(Creating a Log)
下面的例子展示了如何創建日志對象及如何添加日志信息:
~~~
<?php
use Phalcon\Logger;
use Phalcon\Logger\Adapter\File as FileAdapter;
$logger = new FileAdapter("app/logs/test.log");
// These are the different log levels available:
$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"
);
// You can also use the log() method with a Logger constant:
$logger->log(
"This is another error message",
Logger::ERROR
);
// If no constant is given, DEBUG is assumed.
$logger->log(
"This is a message"
);
// You can also pass context parameters like this
$logger->log(
"This is a {message}",
[
'message' => 'parameter'
]
);
~~~
產生的日志信息如下:
~~~
[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
~~~
You can also set a log level using the`setLogLevel()`method. This method takes a Logger constant and will only save log messages that are as important or more important than the constant:
~~~
<?php
use Phalcon\Logger;
use Phalcon\Logger\Adapter\File as FileAdapter;
$logger = new FileAdapter("app/logs/test.log");
$logger->setLogLevel(
Logger::CRITICAL
);
~~~
In the example above, only critical and emergency messages will get saved to the log. By default, everything is saved.
## 事務(Transactions)
保存日志到適配器如文件(文件系統)是非常消耗系統資源的。 為了減少應用性能上的開銷,我們可以使用日志事務。 事務會把日志記錄臨時的保存到內存中然后再 寫入到適配中(此例子中為文件),(這個操作是個原子操作)
~~~
<?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](http://docs.iphalcon.cn/api/Phalcon_Logger.html)也可以同時保存日志信息到多個適配器中:
~~~
<?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"
);
~~~
信息發送的順序和處理器(適配器)注冊的順序相同。
## 信息格式(Message Formatting)
此組件使用 formatters 在信息發送前格式化日志信息。 支持下而后格式:
| 適配器 | 描述 |
| --- | --- |
| [Phalcon\\Logger\\Formatter\\Line](http://docs.iphalcon.cn/api/Phalcon_Logger_Formatter_Line.html) | 文本方式格式化信息 |
| [Phalcon\\Logger\\Formatter\\Firephp](http://docs.iphalcon.cn/api/Phalcon_Logger_Formatter_Firephp.html) | Formats the messages so that they can be sent to FirePHP |
| [Phalcon\\Logger\\Formatter\\Json](http://docs.iphalcon.cn/api/Phalcon_Logger_Formatter_Json.html) | 使用JSON格式格式化信息 |
| [Phalcon\\Logger\\Formatter\\Syslog](http://docs.iphalcon.cn/api/Phalcon_Logger_Formatter_Syslog.html) | 使用系統提供的格式格式化信息 |
### 行格式化處理(Line Formatter)
使用單行格式格式化信息。 默認的格式如下:
~~~
[%date%][%type%] %message%
~~~
我們可以使用`setFormat()`來設置自定義格式。 下面是格式變量:
| 變量 | 描述 |
| --- | --- |
| %message% | 待記錄的日志消息 |
| %date% | 消息添加的時間 |
| %type% | 消息類型(使用大寫) |
下面的例子中展示了如何修改日志格式:
~~~
<?php
use Phalcon\Logger\Formatter\Line as LineFormatter;
$formatter = new LineFormatter("%date% - %message%");
// 修改日志格式
$logger->setFormatter($formatter);
~~~
### 自定義格式處理(Implementing your own formatters)
若要實現自定義的格式則要實現[Phalcon\\Logger\\FormatterInterface](http://docs.iphalcon.cn/api/Phalcon_Logger_FormatterInterface.html)接口, 這樣才能擴展已有的格式或創建自定義的格式
## 適配器(Adapters)
下面的例子中展示了每種適配器的簡單用法:
### 數據流日志記錄器(Stream Logger)
系統日志保存消息到一個已注冊的有效的PHP流中。 這里列出了可用的流: here `\_:
~~~
<?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",
[
"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",
[
"option" => LOG_NDELAY,
"facility" => LOG_MAIL,
]
);
~~~
### FirePHP 日志記錄器(FirePHP Logger)
This logger sends messages in HTTP response headers that are displayed by[FirePHP](http://www.firephp.org/), a[Firebug](http://getfirebug.com/)extension for Firefox.
~~~
<?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"
);
~~~
### 自定義適配器(Implementing your own adapters)
如果開發者想自定義新的日志組件則需實現此接口:[Phalcon\\Logger\\AdapterInterface](http://docs.iphalcon.cn/api/Phalcon_Logger_AdapterInterface.html)。
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl