I'm trying to configure an email logging in Symfony. I followed the cookbook and it works but I have a problem with Fatal errors.
Those errors aren't logged in prod mode. I figured out that when I add Debug::enable(); to app.php, the error get logged, however I still don't get an email.
Here is the relevant configuration:
mail:
type: fingers_crossed
action_level: critical
handler: buffer
buffer:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: %error_mail_from%
to_email: %error_mail_to%
subject: %error_mail_subject%
level: debug
This is not an easy thing to log PHP Fatal Errors, because whenever the error is thrown, PHP shutdown...
However, there is a function that can be used to do a little thing just before the process shut down : register_shutdown_function
Have a look to How do I catch a PHP Fatal Error
This is how Symfony's Debug::enable(); is doing the trick. Have a look to https://github.com/symfony/Debug/blob/master/ErrorHandler.php#L118
Which Symfony version are you using?
Seems like from 2.3 there's a nice improvement that allows you to do that (logging fatal errors). Have a look at this: https://github.com/symfony/symfony/pull/6474
I had the same problem ( fatal erros logged in production but emails not sent) and I managed to make it work adding to my config_prod.php :
services:
mydebug.debug_handlers_listener:
class: Symfony\Component\HttpKernel\EventListener\DebugHandlersListener
arguments:
- { 0:"#http_kernel", 1:'terminateWithException'}
tags:
- { name: kernel.event_subscriber }
I found that such a service is defined in \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\debug.xml but not in debug_prod.xml.
With callback to terminateWithException it works fine in my application.
Related
I am using the fingers crossed handler to buffer log messages until an error occurs. Below is my config:
monolog:
handlers:
buffer:
action_level: error
excluded_http_codes: [401, 403, 404]
handler: logger
type: fingers_crossed
logger:
formatter: monolog.formatter.json
include_stacktraces: true
level: info
path: php://stderr
type: stream
I am finding that exceptions that should match the excluded_http_codes are still being output into my log.
I have dug into the Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy class and am finding that $this->requestStack->getMasterRequest() is returning null by the time the exception reaches the isHandlerActivated method.
Is there something I am clearly doing wrong?
This issue is resolved since updating my composer packages. I'm not sure which package was specifically responsible
Is it possible your errors are occuring in a different handler? If I'm not mistaken you only block a couple errors in your buffer handler and none in your logger handler
I want to log only the deprecated warnings to a new file. But I can't see how to achieve that with monolog. Is there a custom configuration?
Thanks in advance!
All Deprecated Message are logged As INFO level, php Channels so if You try this configuration All deprecation message will be logged in one file
monolog:
handlers:
security:
level: INFO
type: stream
path: '%kernel.logs_dir%/deprecated.log'
channels: [php]
I'm trying to optimize my project logs (Because my log got 3Gb at now) so that when something goes wrong the server send me an email with the details of the error.
I would like to appear in the log only major errors, such as 500 errors, errors that affect the proper functioning of the project.
I have looked at the documentation of the bundle of monolog on the official Symfony2 but I have not been at all clear.
(http://symfony.com/doc/current/reference/configuration/monolog.html)
Could someone tell me how to get this?
We defined
monolog:
handlers:
main:
action_level: error
in our production environment.
Of course you have to check which handler (in our case: 'main') you need to adapt but by changing the action_level to 'error' you get rid of all the debug/info statements in your log and only level 'error' is shown.
Please check your Swift part:
You have level: 'debug' which is obviously not corresponding with your requirement. You should use 'error' or 'critical' instead. See http://symfony.com/doc/current/cookbook/logging/monolog_email.html as well.
Generally it would be helpful to know what kind of log is producing too much and what kind of information? (e.g. we put Doctrine to a different monolog channel to get rid of it in our main log).
Here is my config for monolog.
monolog:
handlers:
main:
action_level: error
console:
type: console
bubble: false
mail:
type: fingers_crossed
action_level: critical
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: his#email.com
to_email: my#email.com
subject: Critical error spotted
level: debug
I've just upgraded from Symfony 2.1 to 2.2. I followed the instructions given here:
http://symfony.com/blog/symfony-2-2-0
https://github.com/symfony/symfony/blob/2.2/UPGRADE-2.2.md
All my code was working beautifully before, but now there is some kind of problem where the framework is injecting "_fragment" into the URL after I try to do something in my application. This is changing my URL from:
http://server/app_dev.php/en-GB/
to:
http://server/app_dev.php/_fragment/
This is then being picked up as the locale by twig and causes it to fail with the following error:
An exception has been thrown during the rendering of a template
("Circular reference detected when adding a fallback catalogue for locale "".")
in "/var/....../HomeBundle/Resources/views/Home/index.html.twig".
2/2 Twig_Error_Runtime: An exception has been thrown during the rendering of a template
("Circular reference detected when adding a fallback catalogue for locale "".")
in "/var/...../HomeBundle/Resources/views/Home/index.html.twig"
1/2 LogicException: Circular reference detected when adding a fallback catalogue for locale "".
Part of the back trace says:
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException".
CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Circular reference detected when adding a fallback catalogue for locale "".") in "/var/...../HomeBundle/Resources/views/Home/index.html.twig"." at /var/...../app/cache/dev/classes.php line 6030
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
When I first login to the application, it all seems to be working fine, the request lists the locale as it should be:
_locale "en-GB"
_route "_home_index"
_route_params "Array(_locale => en-GB)"
However, if I then click on another link (including going back to the same page), it fails with the above error and changes the request to:
_locale "_fragment"
_route "_home_index"
_route_params "Array(_locale => _fragment)"
My routing (for the above example) looks like this:
Home_Home:
resource: "#HomeBundle/Controller/HomeController.php"
type: annotation
prefix: /{_locale}
defaults: { _locale: en }
My config:
framework:
translator: { fallback: en }
default_locale: en
I don't think the problem is the locale handling, but rather with the new fragment sub-handling framework that was added to Symfony 2.2. Possibly it is being called/activated in some way? (or I'm missing some kind of configuration).
I think I found that problem at least, simply adding a fragments configuration item in config.yml fixed that particular issue:
framework:
fragments: { path: /_fragment }
I'm using Symfony2 with the KnpMenuBundle installed as submodules and using the service configuration method.
When calling the page with the menu in the layout using:
{{ knp_menu_render('main') }}
I get the error:
An exception has been thrown during the rendering of a template ("The
menu "main" is not defined.") in CCGlobalBundle::layout.html.twig
My services.yml file (is parsed in the load() method of the CCGlobalExtension.php bundle class) seems to be being called as intentional errors in it cause further code errors:
# src/CC/GlobalBundle/Resources/Config/services.yml
services:
cc_global.menu_builder:
class: CC\GlobalBundle\Menu\MenuBuilder
arguments: ["#knp_menu.factory"]
cc_global.menu.main:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory_service: cc_global.menu_builder
factory_method: createMainMenu
arguments: ["#request"]
scope: request # needed as we have the request as a dependency here
tags:
- { name: knp_menu.menu, alias: main }
And my main /app/config/config.yml file contains the knp_menu: directive:
knp_menu:
twig:
template: knp_menu.html.twig
templating: false # if true, enables the helper for PHP templates
default_renderer: twig # The renderer to use, list is also available by default
There is the same question but no specific solution that worked here: Symfony2 - KnpMenuBundle: Fatal exception in service container
Does anyone know what could be the problem (also checked the Git issues) for this?
I ran into this issue, but found this exception "The menu 'main' is not defined" was thrown because I had not registered the service of MyCompany/MyBundle/Resources/config/services.yml in app/config/config.yml. Fixed like this:
http://symfony.com/doc/current/book/service_container.html
# app/config/config.yml
imports:
- { resource: "#MyCompanyMyBundle/Resources/config/services.yml" }
A code error was introduced to the Git. The git issue is here: https://github.com/KnpLabs/KnpMenuBundle/issues/89
The change for others looking was line 28 of the /vendor/bundles/Knp/Bundle/MenuBundle/DependencyInjection/KnpMenuExtension.php should have read:
$container->getDefinition(sprintf('knp_menu.menu_provider.%s', $builder))->addTag('knp_menu.provider');
while the incorrect code read:
$container->getDefinition(sprintf('knp_menu.menu_provider.%s', $builder))->addTag('knp_menu.menu_provider');
Note 'provider' vs. '*menu_provider*'