elao / error-notifier-bundle
当发生错误时,此捆绑包会发送一个HTML电子邮件。
Requires
- php: >=5.3.2
- swiftmailer/swiftmailer: ~5.0|~6.0
- symfony/console: ~2.3|~3.0|~4.0
- symfony/framework-bundle: ~2.3|~3.0|~4.0
- symfony/twig-bundle: ~2.3|~3.0|~4.0
README
这是什么?
每次生产服务器上出现500错误时,此捆绑包会发送一封电子邮件。您还可以收到404或PHP致命错误的通知。
电子邮件包含大量信息:请参阅README末尾的截图。
安装
Symfony >= 2.1
在您的 composer.json
中添加以下内容
"require": {
"elao/error-notifier-bundle" : "dev-master"
},
然后运行 php composer.phar update elao/error-notifier-bundle
Symfony 2.0.x
将以下行添加到您的 deps
文件中
[ElaoErrorNotifierBundle]
git=git://github.com/Elao/ErrorNotifierBundle.git
target=bundles/Elao/ErrorNotifierBundle
别忘了在您的 app/autoload.php
中注册它
$loader->registerNamespaces(array( ... 'Elao' => __DIR__.'/../vendor/bundles', ));
最后运行 vendors 脚本
$ php bin/vendors install
在 app/AppKernel.php
中注册捆绑包
public function registerBundles() { return array( // ... new Elao\ErrorNotifierBundle\ElaoErrorNotifierBundle(), ); }
配置
在您的 config_prod.yml
文件中添加,您不需要在开发环境中使用错误通知器。
# app/config/config_prod.yml elao_error_notifier: from: from@example.com to: to@example.com handle404: true # default : false handleHTTPcodes: ~ mailer: your.mailer.id # default : mailer handlePHPErrors: true # catch fatal erros and email them handlePHPWarnings: true # catch warnings and email them handleSilentErrors: false # don't catch error on method with an @ filteredRequestParams: [password] # replace request contents of parameter "password" with stars ignoredClasses: ~
如何设置另一个邮件发送器来发送错误邮件
邮件发送器选项已被添加,允许应用程序通过本地SMTP发送错误邮件,而不是使用第三方邮件服务提供商的常规配额。
例如,如果您希望使用一个自定义的邮件发送器,该发送器通过您的服务器邮件传输发送邮件,请在您的项目的 config.yml
中创建此发送器
# app/config/config.yml swiftmailer: default_mailer: default mailers: default: transport: smtp host: localhost username: mylogin password: mypassword notifier: transport: mail
然后只需更改您的 config_prod.yml
中的 mailer
键
# app/config/config_prod.yml elao_error_notifier: mailer: swiftmailer.mailer.notifier
如何忽略给定类引发的错误?
有时,您希望捆绑包不为给定类引发的错误发送电子邮件。您现在可以通过将引发错误的类的名称添加到 ignored_class
键中来实现这一点。
# app/config/config_prod.yml elao_error_notifier: ignoredClasses: - "Guzzle\\Http\\Exception\\ServerErrorResponseException" - ...
如何通过给定的错误代码处理其他HTTP错误?
如果您希望捆绑包为除了500和404以外的其他HTTP错误发送电子邮件,您现在可以指定您想要处理的错误代码列表。
# app/config/config_prod.yml elao_error_notifier: handleHTTPcodes: - 405 - ...
如何避免对同一错误发送许多相同的消息?
如果在拥有大量活跃访问者的网站上发生错误,您将会收到通知器发送的同一错误的大量邮件。
为了避免收到垃圾邮件,请使用 repeatTimeout
选项。
# app/config/config_prod.yml elao_error_notifier: repeatTimeout: 3600
在这个例子中,如果发生错误 X,并且在 1 小时内再次发生相同的错误 X,您将不会收到第二封电子邮件。
Twig 扩展
还有一些扩展您可以在您的 Twig 模板中使用(归功于 Goutte)。
扩展 Twig
{{ "my string, whatever" | pre }} --> wraps with <pre> {{ myBigVar | yaml_dump | pre }} as {{ myBigVar | ydump }} or {{ myBigVar | dumpy }} {{ myBigVar | var_dump | pre }} as {{ myBigVar | dump }} You may control the depth of recursion with a parameter, say foo = array('a'=>array('b'=>array('c','d'))) {{ foo | dumpy(0) }} --> 'array of 1' {{ foo | dumpy(2) }} --> a: b: 'array of 2' {{ foo | dumpy(3) }} --> a: b: - c - d
默认值是 1(MAX_DEPTH 常量)。
如果请求来自任何指定的 IP,如何忽略发送 HTTP 错误?
如果您想忽略来自特定 IP 的请求发送 HTTP 错误,现在您可以指定要忽略的 IP 列表。
# app/config/config_prod.yml elao_error_notifier: ignoredIPs: - "178.63.45.100" - ...
如果用户代理匹配给定的模式,如何忽略发送 HTTP 错误?
由于某些原因,您可能需要忽略来自某些用户代理的通知。通常,您需要与使用人工智能生成 URL 的恼人爬虫一起使用此功能,这些 URL 可能不在您的网站上存在。
# app/config/config_prod.yml elao_error_notifier: ignoredAgentsPattern: "(Googlebot|bingbot)"
如果 URI 匹配给定的模式,如何忽略发送 HTTP 错误?
例如,如果您想忽略所有不存在的图片错误,您可能需要做类似的事情。
# app/config/config_prod.yml elao_error_notifier: ignoredUrlsPattern: "\.(jpg|png|gif)"
如何在请求变量中过滤敏感数据?
有时您不希望通过电子邮件接收密码或其他敏感数据。使用 filteredRequestParams
,您可以指定应替换为星号的请求变量名称。这也适用于命名表单(例如,myFormName[password]
)。
# app/config/config_prod.yml elao_error_notifier: filteredRequestParams: - "password" - "creditCardNo" - "_token"