日志记录Logging

Phalcon提供了一个日志记录组件即 Phalcon\Logger。 我们可以使用此组件输出日志到不同的流中,如文件,系统日志等。 这个组件还提供了其它的功能如日志事务(类似于数据库的事务), 配置选项, 还可以输出不同的格式,另外还支持多种过滤器。 :doc:`Phalcon\Logger <../api/Phalcon_Logger>`提供了多种日志记录方式,从调试程序到跟踪应用的执行以满足应用的需求。

Phalcon\Logger is a component whose purpose is to provide logging services for applications. It offers logging to different backends using different adapters. It also offers transaction logging, configuration options, different formats and filters. You can use the Phalcon\Logger for every logging need your application has, from debugging processes to tracing application flow.

适配器Adapters

此组件使用不同的流适配器来保存日信息。 我们可以按需使用适配器。支持的适配器如下:

This component makes use of adapters to store the logged messages. The use of adapters allows for a common interface for logging while switching backends if necessary. The adapters supported are:

AdapterDescriptionAPI
FileLogs to a plain text filePhalcon\Logger\Adapter\File
StreamLogs to a PHP StreamsPhalcon\Logger\Adapter\Stream
SyslogLogs to the system loggerPhalcon\Logger\Adapter\Syslog
FirephpLogs to the FirePHPPhalcon\Logger\Adapter\FirePHP

创建日志Creating a Log

下面的例子展示了如何创建日志对象及如何添加日志信息:

The example below shows how to create a log and add messages to it:

  1. <?php
  2. use Phalcon\Logger\Adapter\File as FileAdapter;
  3. $logger = new FileAdapter("app/logs/test.log");
  4. $logger->log("This is a message");
  5. $logger->log("This is an error", \Phalcon\Logger::ERROR);
  6. $logger->error("This is another error");

产生的日志信息如下:

The log generated is below:

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

事务Transactions

保存日志到适配器如文件(文件系统)是非常消耗系统资源的。 为了减少应用性能上的开销,我们可以使用日志事务。 事务会把日志记录临时的保存到内存中然后再 写入到适配中(此例子中为文件),(这个操作是个原子操作)

Logging data to an adapter i.e. File (file system) is always an expensive operation in terms of performance. To combat that, you can take advantage of logging transactions. Transactions store log data temporarily in memory and later on write the data to the relevant adapter (File in this case) in a single atomic operation.

  1. <?php
  2. use Phalcon\Logger\Adapter\File as FileAdapter;
  3. // Create the logger
  4. $logger = new FileAdapter("app/logs/test.log");
  5. // Start a transaction
  6. $logger->begin();
  7. // Add messages
  8. $logger->alert("This is an alert");
  9. $logger->error("This is another error");
  10. // Commit messages to file
  11. $logger->commit();

使用多个处理程序进行日志记录Logging to Multiple Handlers

:doc:`Phalcon\Logger <../api/Phalcon_Logger>`也可以同时保存日志信息到多个适配器中:

Phalcon\Logger can send messages to multiple handlers with a just single call:

  1. <?php
  2. use Phalcon\Logger,
  3. Phalcon\Logger\Multiple as MultipleStream,
  4. Phalcon\Logger\Adapter\File as FileAdapter,
  5. Phalcon\Logger\Adapter\Stream as StreamAdapter;
  6. $logger = new MultipleStream();
  7. $logger->push(new FileAdapter('test.log'));
  8. $logger->push(new StreamAdapter('php://stdout'));
  9. $logger->log("This is a message");
  10. $logger->log("This is an error", Logger::ERROR);
  11. $logger->error("This is another error");

信息发送的顺序和处理器(适配器)注册的顺序相同。

The messages are sent to the handlers in the order they were registered.

信息格式Message Formatting

此组件使用 formatters 在信息发送前格式化日志信息。 支持下而后格式:

This component makes use of ‘formatters’ to format messages before sending them to the backend. The formatters available are:

AdapterDescriptionAPI
LineFormats the messages using a one-line stringPhalcon\Logger\Formatter\Line
JsonPrepares a message to be encoded with JSONPhalcon\Logger\Formatter\Json
SyslogPrepares a message to be sent to syslogPhalcon\Logger\Formatter\Syslog

行格式化处理Line Formatter

使用单行格式格式化信息。 默认的格式如下:

Formats the messages using a one-line string. The default logging format is:

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

我们可以使用setFormat()来设置自定义格式。 下面是格式变量:

You can change the default format using setFormat(), this allows you to change the format of the logged messages by defining your own. The log format variables allowed are:

VariableDescription
%message%The message itself expected to be logged
%date%Date the message was added
%type%Uppercase string with message type

下面的例子中展示了如何修改日志格式:

The example below shows how to change the log format:

  1. <?php
  2. use Phalcon\Logger\Formatter\Line as LineFormatter;
  3. //Changing the logger format
  4. $formatter = new LineFormatter("%date% - %message%");
  5. $logger->setFormatter($formatter);

自定义格式处理Implementing your own formatters

若要实现自定义的格式则要实现 :doc:`Phalcon\Logger\FormatterInterface <../api/Phalcon_Logger_FormatterInterface>`接口, 这样才能扩展已有的格式或创建自定义的格式

The Phalcon\Logger\FormatterInterface interface must be implemented in order to create your own logger formatter or extend the existing ones.

适配器Adapters

下面的例子中展示了每种适配器的简单用法:

The following examples show the basic use of each adapter:

数据流日志记录器Stream Logger

系统日志保存消息到一个已注册的有效的PHP流中。 这里列出了可用的流: here:

The stream logger writes messages to a valid registered stream in PHP. A list of streams is available here:

  1. <?php
  2. use Phalcon\Logger\Adapter\Stream as StreamAdapter;
  3. // Opens a stream using zlib compression
  4. $logger = new StreamAdapter("compress.zlib://week.log.gz");
  5. // Writes the logs to stderr
  6. $logger = new StreamAdapter("php://stderr");

文件日志记录器File Logger

文件适配器保存所有的日志信息到普通的文件中。 默认情况下日志文件使用添加模式打开,打开文件后文件的指针会指向文件的尾端。 如果文件不存在,则会尝试创建。 我们可以通过传递附加参数的形式来修改打开的模式:

This logger uses plain files to log any kind of data. By default all logger files are opened using append mode which opens the files for writing only; placing the file pointer at the end of the file. If the file does not exist, an attempt will be made to create it. You can change this mode by passing additional options to the constructor:

  1. <?php
  2. use Phalcon\Logger\Adapter\File as FileAdapter;
  3. // Create the file logger in 'w' mode
  4. $logger = new FileAdapter("app/logs/test.log", array(
  5. 'mode' => 'w'
  6. ));

Syslog 日志记录器Syslog Logger

使用系统日志适配器。 由于操作系统的不同得到的日志也不尽相同:

This logger sends messages to the system logger. The syslog behavior may vary from one operating system to another.

  1. <?php
  2. use Phalcon\Logger\Adapter\Syslog as SyslogAdapter;
  3. // Basic Usage
  4. $logger = new SyslogAdapter(null);
  5. // Setting ident/mode/facility
  6. $logger = new SyslogAdapter("ident-name", array(
  7. 'option' => LOG_NDELAY,
  8. 'facility' => LOG_MAIL
  9. ));

FirePHP 日志记录器FirePHP Logger

发送消息到FirePHP:

This logger sends messages in HTTP response headers that are displayed by FirePHP, a Firebug extension for Firefox.

  1. <?php
  2. use Phalcon\Logger\Adapter\Firephp as Firephp;
  3. $logger = new Firephp("");
  4. $logger->log("This is a message");
  5. $logger->log("This is an error", \Phalcon\Logger::ERROR);
  6. $logger->error("This is another error");

自定义适配器Implementing your own adapters

如果开发者想自定义新的日志组件则需实现此接口: Phalcon\Logger\AdapterInterface

The Phalcon\Logger\AdapterInterface interface must be implemented in order to create your own logger adapters or extend the existing ones.