dziki / monolog-sentry-bundle
通过用户、用户代理、修订和Symfony版本数据改进Sentry日志记录
Requires
- php: >=7.1
- monolog/monolog: ^1
- psr/simple-cache: ^1.0
- psr/simple-cache-implementation: ^1.0
- sentry/sentry: ^1.9.1
- symfony/dependency-injection: ^4.2|^5.0
- symfony/monolog-bridge: ^4.2|^5.0
- symfony/monolog-bundle: ^3.3
Requires (Dev)
- donatj/phpuseragentparser: ^0.11
- friendsofphp/php-cs-fixer: ^2.12
- matthiasnoback/symfony-dependency-injection-test: dev-master
- nyholm/symfony-bundle-test: ^1.4
- symfony/phpunit-bridge: ^4.1.8
- symfony/security-bundle: ^4.2|^5.0
- symfony/string: ^5.0
Suggests
- donatj/phpuseragentparser: Basic package for parsing User-Agent header, faster than native browscap
- symfony/security-bundle: Required for current user data appending to logs
Conflicts
- donatj/phpuseragentparser: <0.11
This package is auto-updated.
Last update: 2023-03-01 00:29:53 UTC
README
为Monolog日志记录添加有用数据的Bundle,例如用户名、解析的用户代理头部、主机名、Symfony版本、提交哈希等 - 您可以为所有日志提供自定义标签。
安装
使用composer require dziki/monolog-sentry-bundle
命令安装Bundle。
TL;DR
默认的monolog raven handler
与开启和关闭Bundle的sentry/sentry
包客户端对相同错误处理的比较,配置了一些基本配置。如您所见,在开启Bundle后,浏览器、用户、面包屑和有用的标签显示出来,使错误日志更容易阅读。
之前
之后
启用Bundle
向config/bundles.php
中添加条目
return [ // ... Dziki\MonologSentryBundle\MonologSentryBundle::class => ['all' => true], ];
或到app/AppKernel.php
<?php // app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Dziki\MonologSentryBundle\MonologSentryBundle(), ); // ... } // ... }
配置
默认配置不做任何事情,您需要根据需要手动调整
monolog_sentry: user_context: false # append username from TokenStorage to log user_agent_parser: false # install donatj/phpuseragentparser package # and set to 'phpuseragent' to parse browser name, version and platform from user agent
您可以通过将值设置为true
来开启日志用户上下文 - 这需要symfony/security-bundle
包。需要user_agent_parser
作为值的有效用户代理头部解析服务。解析用户代理每请求花费约0.1毫秒(使用原生解析器可达1毫秒),因此...
缓存已解析的用户代理
在提供实现Psr\SimpleCache\CacheInterface
服务的cache
配置条目下支持缓存。从Symfony 4.1版本开始,有默认的简单缓存服务cache.app.simple
,在之前的版本中,您需要定义自己的服务
monolog_sentry: cache: cache.app.simple # service implementing "Psr\SimpleCache\CacheInterface" interface
自定义标签
您可以通过添加自定义标签来扩展日志数据的数量。例如,为了记录Symfony版本、有用的Sentry环境和服务器名称,您应该将配置修改为以下内容
monolog_sentry: ... tags: symfony_version: !php/const Symfony\Component\HttpKernel\Kernel::VERSION # useful for regression check commit: '%env(APP_REVISION)%' # for example hash of commit, set your own environment variable or parameter environment: '%env(SERVER_NAME)%' # Sentry environment discriminator, much more useful than default `prod`
完整的基本配置
monolog_sentry: user_context: true # append username from TokenStorage to log user_agent_parser: phpuseragent # parse browser name, version and platform from user agent cache: cache.app.simple # service implementing "Psr\SimpleCache\CacheInterface" interface, since SF 4.1 tags: symfony_version: !php/const Symfony\Component\HttpKernel\Kernel::VERSION # useful for regression check commit: '%env(APP_REVISION)%' # for example hash of commit, set your own environment variable or parameter environment: '%env(SERVER_NAME)%' # Sentry environment discriminator, much more useful than default `prod`
用户代理解析器
Bundle支持两种解析器
phpuseragent
(github.com/donatj/PhpUserAgent) - 建议的包native
(get_browser()) - 在php.ini中,browscap配置设置必须指向正确的browscap.ini文件位置
可以通过user_agent_parser
值进行配置,分别是phpuseragent
或native
。您还可以添加自己的,通过提供实现ParserInterface的服务名称。
提示
- 将
stop_buffering: false
添加到您的fingers_crossed
处理程序中,以将低级消息通知作为面包屑
monolog: handlers: main: type: fingers_crossed action_level: error handler: buffered stop_buffering: false sentry: type: raven dsn: '%env(SENTRY_DSN)%' level: info # logs which will be shown as breadcrumbs in Sentry issue release: 1.0.0
- 将Sentry处理程序的
release
选项添加到monolog配置中,以便于回归寻找
monolog: handlers: ... sentry: ... release: '%env(APP_VERSION)%' # version tag or any release ID
许可证
MonologSentryBundle在MIT许可证下发布。