Symfony2 projet not working in prod with JSMserializerbundle and DoctrineMongoDBBundle - php

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.

Related

Log from Symfony to php://stdout shows log messages in my console commands

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

symfony dev.log in prod

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.

Sonata block http cache handler sonata.cache.noop not defined

I'm trying to set up sonata block cache but when I setup the http_cache handler I'm getting an exception even when the service seems to be there.
My current test setup is the following
sonata_cache:
caches:
predis:
servers:
- {host: %redis_host%, port: %redis_port%, database: 2}
apc:
token: %secret% # token used to clear the related cache
prefix: %domain% # prefix to ensure there is no clash between instances
# servers:
# - { domain: %domain%, ip: 127.0.0.1, port: 80 }
timeout:
RCV: { sec: 5, usec: 0 }
SND: { sec: 5, usec: 0 }
sonata_block:
http_cache:
# handler: sonata.cache.noop
listener: true
I can see the multiple services that I enabled on the sonata_cache.caches
$ app/console debug:container sonata.cache.noop
[container] Information for service sonata.cache.noop
Service Id sonata.cache.noop
Class Sonata\Cache\Adapter\Cache\NoopCache
Tags
- sonata.cache ()
[...]
If I change the sonata_block setup to:
sonata_block:
http_cache:
handler: sonata.cache.noop
listener: true
I got the following exception
InvalidArgumentException in ContainerBuilder.php line 796 "The service
definition "sonata.cache.noop" does not exist."
Does anyone had a similar issue?
Try to replace :
handler: sonata.cache.noop
to :
handler: sonata.block.cache.handler.noop

Assetic:dump error Assetic\Filter\LessFilter

I am working in Windows and trying to deplay my project in prod on Debian server. I am using svn. I changed my config.yml to handle the path of node and less.
When I try to do :php app/console assetic:dump --env=prod on the server I receive an Error : PHP Catchable fatal error: Argument 2 passed to Assetic\Filter\LessFilter::__construct() must be of the type array, string given, called in /var/www/ales/app/cache/prod/appProdProjectContainer.php on line 549 and defined in /var/www/ales/vendor/kriswallsmith/assetic/src/Assetic/Filter/LessFilter.php on line 54
Herewith my configuration :
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: ['MyAppliCoreBundle']
java: /usr/bin/java
filters:
cssrewrite: ~
cssembed:
jar: %kernel.root_dir%/resources/java/cssembed-0.4.5.jar
less:
node: /usr/lib/nodejs
node_paths: [/usr/lib/node_modules]
apply_to: "\.less$"
cssrewrite: ~
yui_css:
jar: "%kernel.root_dir%/ressources/java/yuicompressor-2.4.8.jar"
assets:
bootstrap_css:
inputs:
- %kernel.root_dir%/../vendor/twbs/bootstrap/less/bootstrap.less
filters:
- less
- cssrewrite
jquery:
inputs:
- %kernel.root_dir%/../vendor/jquery/jquery/jquery-2.1.1.js
output: js/jquery.js
When I try on DEV I get this error :
22:45:00 [file+] /var/www/ales/app/../web/assetic/bootstrap_css.less
[Assetic\Exception\FilterException]
An error occurred while running:
'/usr/lib/nodejs' '/tmp/assetic_lessBjeLzE'
Error Output:
sh: 1: /usr/lib/nodejs: Permission denied
Input:
// Core variables and mixins
#import "variables.less";
#import "mixins.less";
I checked permission and set to 777 /usr/lib/nodejs but nothing change.
If you need more details, don't hesitate.
Symfony2 with bootstrap 3 on debian.
which node return :
/usr/local/bin/node
wich nodejs return :
/usr/bin/nodejs
and echo $NODE_PATH return :
/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
Thanks a lot for your help, I was planning to set in prod this weekend...
I had the same problem and solved it well, first you can try with this configuration:
less:
node: "node"
node_paths:
- "/usr/lib/node_modules"
- "%kernel.root_dir%/../../../node_modules"
if later you have this Error: Cannot find module 'less' you have to install the module:
npm install -g less
I hope I have been helpful!!

Graylog2 with Symfony 2 (Monolog)

I want to use the "Monolog -> GelfHandler" in a SF2 project. So I've added the following to my composer.json:
"graylog2/gelf-php": "dev-master"
My SF2 config.yml:
monolog:
handlers:
main:
type: gelf
publisher:
hostname: %Graylog2.Host%
port: %Graylog2.Port%
level: info
formatter: monolog.formatter.session_request
But now, if I want to log something, I get an error in GelfHandler::write()
Gelf\Publisher::publish() must be an instance of Gelf\MessageInterface, string given
What is wrong here?
ok, i will answer my question by my self. the failure is to change the "formatter" or you have to use a formatter is sub-classes from Monolog\Formatter\GelfMessageFormatter.
thx # all

Categories