dakenf / release-profiler-bundle
此 Bundle 提供通过 slack 或电子邮件进行请求日志记录和扩展错误报告
Requires
- php: ^5.4.0|^7.0
- doctrine/orm: ~2.4|~2.5
- guzzlehttp/guzzle: ~5.0|~6.0
- jdorn/sql-formatter: *
- symfony/framework-bundle: ~2.7|~3.0
Requires (Dev)
- matthiasnoback/symfony-config-test: ^2.0
- matthiasnoback/symfony-dependency-injection-test: ^1.0
- phpunit/phpunit: ^4.8|^5.5
- predis/predis: ^1.0
- snc/redis-bundle: 2.x-dev
- sonata-project/admin-bundle: 3.4.*
- symfony/dependency-injection: ~2.6|~3.0
- twig/twig: ^1.18
This package is not auto-updated.
Last update: 2024-09-14 19:47:32 UTC
README
简介
此 Bundle 允许您记录您 symfony 应用程序的所有请求和响应,立即将所有错误发布到 slack,并查看这些请求上发生的数据库查询。
它还显示所有已记录数据库查询的完整堆栈跟踪。如果您使用 SonataAdminBundle,可以轻松显示和筛选请求、查询和错误。
在每个请求上,此 Bundle 创建 Daken\ReleaseProfilerBundle\Request 对象,检查是否应使用 log_conditions.request 配置数组将其持久化。如果发生任何错误,则将其添加到请求并发布到 Slack 或您的自定义通知器。
每个数据库查询也被添加到请求中。您可以通过配置 database_query_time_log_threshold 参数来仅记录持续时间超过特定时间的查询。
当内核终止时,将 Request 对象传递给配置的 PersistManager。它可以是一个 redis、数据库或您自己的实现 PersistManagerInterface 的服务。当使用 redis 时,将请求对象序列化并推送到 redis 中的队列。之后,使用 daken:profiler:flush
命令将其持久化到数据库。
强烈推荐使用 redis,因为它不会为您的每个请求添加显著的开销。
安装
步骤 1:下载 Bundle
要安装
$ composer require dakenf/release-profiler-bundle "~1"
步骤 2:启用 Bundle
然后,通过将其添加到项目中 app/AppKernel.php
文件中注册的 Bundle 列表中启用该 Bundle
<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Daken\ReleaseProfilerBundle\DakenReleaseProfilerBundle(),
// ...
);
}
}
.. _安装章节
:https://getcomposer.org.cn/doc/00-intro.md
步骤 3:配置
使用场景 1:仅将错误记录到数据库并发布到 slack,不使用 redis
在这种情况下,您应该为此 Bundle 定义另一个实体管理器,因为当 doctrine 在使用 EM 时遇到任何数据库错误,它会永久关闭它。因此,代码将无法持久化请求数据。
# app/config.yml
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
# this is our second entity manager. it can use same connection
profiler:
connection: default
mappings:
DakenReleaseProfilerBundle: ~
daken_release_profiler:
persist_manager: database
entity_manager: doctrine.orm.profiler_entity_manager # here we have our custom entity manager
log_conditions:
request:
- { exclude: true, error: 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' }
- { error: true }
request_body:
- { error: true }
response_body:
- { error: true }
enable_sonata: true
error_notifier: slack
slack:
hook_url: %your_custom_slack_hook_url_parameter%
username: Rage bot
emoji: ":rage4:"
使用场景 2:仅将错误记录到数据库并发布到 slack,不使用 redis。但也在 API 服务器上记录所有请求和响应。
# app/config.yml
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
# this is our second entity manager. it can use same connection
profiler:
connection: default
mappings:
DakenReleaseProfilerBundle: ~
daken_release_profiler:
persist_manager: database
entity_manager: doctrine.orm.profiler_entity_manager # here we have our custom entity manager
log_conditions:
request:
- { exclude: true, error: 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' }
- { error: true }
- { host: "%api_host%" }
request_body:
- { error: true }
- { host: "%api_host%" }
response_body:
- { error: true }
- { host: "%api_host%" }
enable_sonata: true
error_notifier: slack
slack:
hook_url: %your_custom_slack_hook_url_parameter%
username: Rage bot
emoji: ":rage4:"
使用场景 3:记录所有请求(除 404 外),仅在 API 服务器上保存请求和响应体。使用 SncRedisBundle 和 redis 保存请求数据。排除 sonata admin 和 symfony debug toolbar 请求。记录持续时间超过 50 毫秒的数据库查询。
# app/config.yml
daken_release_profiler:
persist_manager: redis
redis:
service: "snc_redis.default"
log_conditions:
request:
- { exclude: true, error: 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' }
- { exclude: true, path_preg: "#^/admin#"}
- { exclude: true, route: "_wdt"}
- { exclude: true, route: "_profiler"}
- { always: true }
request_body:
- { host: "%api_host%" }
- { error: true }
response_body:
- { host: "%api_host%" }
- { error: true }
enable_sonata: true
database_query_time_log_threshold: 50
error_notifier: slack
slack:
hook_url: %your_custom_slack_hook_url_parameter%
username: Rage bot
emoji: ":rage4:"
您可能还想为应能够查看分析数据的用户添加角色
# app/securtiy.yml
ROLE_SOME_USER:
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_REQUEST_LIST
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_REQUEST_VIEW
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_ERROR_LIST
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_ERROR_VIEW
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_DATABASE_QUERY_LIST
- ROLE_DAKEN_RELEASE_PROFILER_ADMIN_DATABASE_QUERY_VIEW
步骤 4:创建数据库表
如果您已为分析器定义了另一个实体管理器,现在想获取创建表的 SQL 查询,应在命令中添加 --em=profiler
,其中 profiler 是实体管理器的名称。
$ php app/console doctrine:schema:update --dump-sql --em=profiler
或
$ php app/console doctrine:migrations:diff --em=profiler
步骤 5:运行 flush 命令
此步骤仅在您使用 redis 作为持久管理器时需要。
您应该考虑使用 redis,因为数据库持久管理器将为您的每个请求添加开销,然而在小型项目中它不会产生很大影响。
您可以使用 cron 或将其作为服务启动来运行 flush 命令。
$ php app/console daken:profiler:flush -d --silent
-d 以守护进程方式运行
--silent 超越所有输出
--wait-seconds 设置阻塞请求到 redis 的秒数。此超时在守护进程模式下使用,以使代码等待 redis 队列中有新请求。如果使用 SncRedisBundle 和 phpredis,请确保您的 redis 连接配置超时设置大于此值。否则您将收到 RedisException(超时)。