I've similar quesitons here in stackoverflow but haven't seen an answer.
I currently have dev.log and prod.log in my production environment and I don't understand why. I should only have prod.log.
My dev.log only contains this (repeated every 2 minutes):
[2017-04-21 17:50:02] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". [] []
[2017-04-21 17:50:02] doctrine.DEBUG: SELECT e0_.id AS id0, e0_.name AS name1, e0_.progress AS progress2, e0_.path AS path3, e0_.data AS data4, e0_.created_at AS created_at5, e0_.updated_at AS updated_at6, (SELECT COUNT(e1_.id) AS dctrn__1 FROM export e1_ WHERE e1_.progress > 0 AND e1_.progress < 100) AS sclr7, e0_.organization_id AS organization_id8, e0_.owner_id AS owner_id9 FROM export e0_ WHERE e0_.progress = 0 HAVING sclr7 = 0 LIMIT 1 [] []
[2017-04-21 17:50:02] event.DEBUG: Notified event "console.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate". [] []
Here are my config_dev.yml and config_prod.yml monolog configs:
config_dev:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
config_prod:
monolog:
handlers:
main_critical:
type: fingers_crossed
action_level: critical
handler: buffered_critical
buffered_critical:
type: buffer
handler: swift_critical
swift_critical:
type: swift_mailer
from_email: %error_mail_sender%
to_email: %error_mail_recipients%
subject: An error occurred
level: debug
main_error:
type: fingers_crossed
action_level: error
handler: grouped_error
grouped_error:
type: group
members: [streamed]
streamed:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
I notice the log shows that a console command happens, and there's a cronjob which runs a command every two minutes, but I don't know why this goes to dev.log...
Thanks a lot for your help,
Cheers
By Default, Symfony Commands are executed in the Dev environment that's why it stores all kinds of logs in the dev.log file using your dev environment configurations for logging.
you need to use --env=prod flag with your Symfony command to explicitly tell Symfony to run this command in prod environment.
NOTE:
so if you are running command via cron add the --env=prod flag at the end of a command.
this answer is an explanation of #cerad comment.
Related
I am running a docker container with the official php:7.3.3-apache tag. I have Symfony 4 installed with monolog enabled.
Normally monolog uses the var/dev.log, var/prod.log and var/test.log to write its logs but you can change the configuration to php://stdout or php://stderr to send the logs to the docker container.
My monolog.yaml file looks like:
monolog:
handlers:
main:
type: stream
path: "php://stdout"
level: debug
channels: ["!event"]
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
And it sends the logs properly from the website, however when I run a command using the php bin/console command I get the logs right on the screen, for example:
$ php bin/console cache:clear
[2019-06-24 14:29:19] doctrine.DEBUG: SELECT h0_.id AS id_0, h0_.name AS name_1, h0_.value AS value_2, h0_.description AS description_3 FROM hpa_configuration h0_ [] []
[2019-06-24 14:29:19] php.INFO: User Deprecated: The "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" class is deprecated since Symfony 4.2, use "Symfony\Component\Console\Command\Command" with dependency injection instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: The \"Symfony\\Bundle\\FrameworkBundle\\Command\\ContainerAwareCommand\" class is deprecated since Symfony 4.2, use \"Symfony\\Component\\Console\\Command\\Command\" with dependency injection instead. at /var/www/html/hpa/vendor/symfony/framework-bundle/Command/ContainerAwareCommand.php:18)"} []
// Clearing the cache for the dev environment with debug ......
How can I log properly from the website and commands?
Change php://stdout to php://stderr
In my Symfony app I am using monolog to log errors and I got different channels. I need to write different channels to different files. I got the following monolog config:
//app\config\config.yml
monolog:
channels: ['my_channel', 'my_channel2']
use_microseconds: false
handlers:
file:
type: stream
path: %kernel.logs_dir%/prod_info.log
level: info
channels: [!my_channel2]
file_2:
type: stream
path: %kernel.logs_dir%/mylogfile_2.log
level: info
channels: [my_channel2]
file_errors:
type: stream
path: %kernel.logs_dir%/cms_errors.log
level: error
channels: [!my_channel2]
my config.prod config
monolog:
handlers:
main:
type: fingers_crossed
action_level: debug
handler: nested
channels: [!my_channel2]
nested:
type: stream
path: "%kernel.logs_dir%/main_%kernel.environment%.log"
level: debug
channels: [!my_channel2]
console:
type: console
I can access my_channel2 in the code like this:
$logger = $this->get('monolog.logger.my_channel2');
$logger->debug('Some message here');
but still all my_channel2 messages are written to main_prod.log file instead of mylogfile_2.log. Any ideas how to fix it would be welcome. Thank you.
just needed to change action level from info to debug for the required channel
file_2:
type: stream
path: %kernel.logs_dir%/mylogfile_2.log
level: debug // <== change from info to debug here
channels: [my_channel2]
I have configured logger for different channel in different files, but it does not work for me. It's work but it writes in console not in pointed file.
And I need write log to file in channel search.
Here is my code:
#app/config/config_dev.yml
monolog:
handlers:
search:
type: stream
level: error
path: "%kernel.logs_dir%/search_log.log"
channels: [search]
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: [!event, !search]
console:
type: console
channels: [!event, !doctrine, !search]
Defined service:
#MyBundle/Resources/config/services.yml
services:
app.logger_search:
class: Symfony\Bridge\Monolog\Logger
arguments: ["#logger"]
tags:
- {name: monolog.logger, channel: search}
Now use it service, try to test it:
#MyController.php
/**
* #Route("/test")
*/
public function test()
{
$this->get("app.logger_search")->error("Test");
return $this->json("test");
}
But it writes into console insted of file.
Console I meant where I ran my server: php bin\console server:run.
Creating your own Channel. This is done either via the configuration or by tagging your service with monolog.logger and specifying which channel the service should log to (just as you have done).
Both ways are valid and in both cases you logger will be named:
monolog.logger.<you-channel-name>
So use monolog.logger.search instead of your service id app.logger_search to fix the issue.
I you don't have a strong reason to change the logger behavior, I suggest to configure additional channels without tagged services:
# app/config/config.yml
monolog:
channels: ['foo', 'bar']
With this, you can now send log messages to the foo channel by using the automatically registered logger service monolog.logger.foo.
During the execution of Symfony Commands, I want to log messages to a different file. I have read the Symfony and Monolog documentation, and it should work like I describe here. (Note that I know messages from the 'doctrine', 'event', ... channels will still be logged by the main handler, but that doesn't matter for me)
In my config.yml, I have this:
monolog:
channels: [commandline]
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.main.log"
level: debug
channels: [!commandline]
commandline:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.commandline.log"
level: debug
channels: commandline
stdout:
type: stream
path: "php://stdout"
level: debug
channels: commandline
mail:
type: stream
action_level: alert
handler: buffered_mail
buffered_mail:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: some#email.com
to_email: some#email.com
subject: "Something went wrong"
level: alert
I'm expecting to have 2 log-files: dev.main.log and dev.commandline.log.
But I'm still having a third log-file: dev.log that logs all messages.
I don't seem to find where that loghandler is defined and how I can prevent it from logging things...
If anyone could point me in the right direction, that would be nice!
btw, i'm using:
symfony 2.3
monolog-bundle 2.4
EDIT
There is no monolog section in the config_dev.yml
REMOVE monolog.handlers.mainfrom config_dev.yml.
It usally contains path: "%kernel.logs_dir%/%kernel.environment%.log"
config _dev.yml (default)
monolog:
handlers:
main: # <- remove this handler
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log" #<- logs/dev.log
level: debug
Remove the main handler from this config file.
If anyone comes across this and is still interested in why this happens, the debug handler is injected in the \Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DebugHandlerPass::process() method...
class DebugHandlerPass implements CompilerPassInterface
{
// ...
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('profiler')) {
return;
}
if (!$container->getParameter('kernel.debug')) {
return;
}
$debugHandler = new Definition('%monolog.handler.debug.class%', array(Logger::DEBUG, true));
$container->setDefinition('monolog.handler.debug', $debugHandler);
foreach ($this->channelPass->getChannels() as $channel) {
$container
->getDefinition($channel === 'app' ? 'monolog.logger' : 'monolog.logger.'.$channel)
->addMethodCall('pushHandler', array(new Reference('monolog.handler.debug')));
}
}
}
As you can see, this pushes a new handler on to every registered channel, thus overriding any other handlers that might already have been added.
My symfony2 projet works flawlessly in the dev environment. The problem occurs when I switch to the production environment. When I call a controller which have an array of entities and serialize it to json with the JSMserializer, it crashes. The client still receive a response but the json is empty, not even a "[]". When I take a look in the logs I can read these errors:
security.INFO: Populated SecurityContext with an anonymous Token [] []
request.INFO: Matched route "employe_index" (parameters: "_controller": "Pisteur\WebRefugeManager\Bundle\CoreBundle\Controller\EmployeController::indexAction", "_route": "employe_index") [] []
request.CRITICAL: Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "#proxy" in method Doctrine\MongoDB\Cursor::current() was never imported. Did you maybe forget to add a "use" statement for this annotation? (uncaught exception) at /home/wpdemo/symfony/vendor/doctrine-common/lib/Doctrine/Common/Annotations/AnnotationException.php line 52 [] []
I've searched the errors in google and I found these two topics:
https://github.com/doctrine/mongodb-odm/issues/255
https://github.com/schmittjoh/JMSSerializerBundle/issues/51
These topics didn't help me and I really dont understand the problem. Anyone has an idea?
[EDIT]
Here is my configs:
DEV:
imports:
- { resource: config.yml }
framework:
router: { resource: "%kernel.root_dir%/config/routing_dev.yml" }
profiler: { only_exceptions: false }
web_profiler:
toolbar: true
intercept_redirects: false
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
firephp:
type: firephp
level: info
assetic:
use_controller: true
PROD:
imports:
- { resource: config.yml }
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
[EDIT]
This is the commands I run:
php app/console cache:clear --env=prod --no-debug
php app/console cache:warmup
php app/console assets:install web
php app/console assetic:dump web --env=prod --no-debug
Had a similar issue once, when deploying the first time with Capifony. I was only able to solve it by manually clearing app/cache/prod folder. Not using cache:clear, but actually removing the folder,
rm -rf app/cache/prod
I have yet to find out why cache:clear --env=prod sometimes wont clear it correctly, it may be a permission issue.