Symfony 日志记录教程
最后修改时间:2025年3月3日
Symfony 日志记录教程展示了如何在 Symfony 7.2 中使用 Monolog 进行日志记录。我们配置 Monolog,创建自定义日志通道,并在控制器和服务中使用日志记录。
Symfony
Symfony 是一套可重用的 PHP 组件,也是一个用于 Web 项目的 PHP 框架。Symfony 于 2005 年发布为自由软件。Fabien Potencier 是 Symfony 的最初作者。Symfony 在很大程度上受到了 Spring 框架的启发。
Monolog
Monolog 是一个 PHP 日志库。它是 Symfony 中默认使用的日志库。Monolog 支持多种处理器(handlers)、格式化器(formatters)和处理者(processors),使其非常灵活。
设置项目
我们首先创建一个新的 Symfony 项目并安装必要的依赖项。
$ composer create-project symfony/skeleton symfony-logging "^7.2" $ cd symfony-logging
我们创建一个新的 Symfony 7.2 项目并导航到项目目录。
$ composer require symfony/monolog-bundle
我们安装 Monolog 捆绑包,它默认已包含在 Symfony 中。此步骤可确保捆绑包得到正确配置。
配置 Monolog
Monolog 在 config/packages/monolog.yaml 文件中进行配置。我们自定义配置以创建自定义日志通道和处理器。
monolog:
channels:
- app
- security
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
app:
type: stream
path: "%kernel.logs_dir%/app.log"
level: info
channels: ["app"]
security:
type: stream
path: "%kernel.logs_dir%/security.log"
level: warning
channels: ["security"]
此配置: - 创建了两个自定义日志通道:app 和 security。 - 将 app 消息以 info 级别记录到 app.log。 - 将 security 消息以 warning 级别记录到 security.log。 - 将所有其他消息记录到默认日志文件中。
在控制器中使用日志记录
我们创建一个控制器来演示日志记录。
$ php bin/console make:controller LogController
我们生成一个 LogController。
<?php
declare(strict_types=1);
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class LogController extends AbstractController
{
#[Route('/log', name: 'app_log')]
public function index(LoggerInterface $logger): Response
{
// Log messages using the default logger
$logger->debug('This is a debug message.');
$logger->info('This is an info message.');
$logger->warning('This is a warning message.');
$logger->error('This is an error message.');
$logger->critical('This is a critical message.');
// Log messages using custom channels
$logger->info('This is an app-specific message.', ['channel' => 'app']);
$logger->warning('This is a security-specific message.', ['channel' => 'security']);
return new Response('Logging example. Check your log files.');
}
}
LogController 使用默认日志记录器和自定义通道记录消息。LoggerInterface 被注入到控制器中。
在服务中使用日志记录
我们创建一个服务来演示在非控制器场景中使用日志记录。
<?php
declare(strict_types=1);
namespace App\Service;
use Psr\Log\LoggerInterface;
class ExampleService
{
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function doSomething(): void
{
$this->logger->info('Doing something important in the service.');
}
}
当调用 doSomething 方法时,ExampleService 会记录一条消息。
测试日志记录
我们启动 Symfony 开发服务器并测试日志记录功能。
$ php bin/console server:start
我们启动开发服务器。
$ curl localhost:8000/log
我们访问 /log 路由来触发控制器中的日志记录。
检查 var/log/ 目录中的日志文件: - dev.log:所有消息的默认日志文件。 - app.log:app 通道消息的自定义日志文件。 - security.log:security 通道消息的自定义日志文件。
高级日志配置
Monolog 支持高级配置,例如: - 根据严重程度记录到不同的文件。 - 将日志发送到外部服务(例如,Slack、Elasticsearch)。 - 使用处理器为日志记录添加额外数据。
monolog:
handlers:
slack:
type: slack
token: "xoxb-your-slack-token"
channel: "#logs"
level: critical
此配置将关键日志发送到 Slack 通道。
在本教程中,我们在 Symfony 7.2 中使用了 Monolog 进行日志记录。我们配置了自定义日志通道,在控制器和服务中使用了日志记录,并探索了高级日志选项。
列出 所有 Symfony 教程。