ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[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` 接口才能创建自己的记录器适配器或扩展现有的记录器适配器。