Symfony 4, how print logs in profiler log page? - symfony

From php files in Symfony 4, how can I print variables (for debug) to the log profiler page ?
I mean this page :
I tried that :
$log = new Logger()
$log->info("test");
$log2 = new ConsoleLogger(OutputInterface::OUTPUT_NORMAL);
$log2->alert("hello !");
But nothing appears...

You may not have installed the monolog bundle. Without that you get a very rudimentary logging.
Run:
composer require symfony/monolog-bundle
Reload app and see if message start appearing in the log tab of the profiler.

I just follow the monolog symfony guide : http://symfony.com/doc/current/logging.html
After logger install, I add somewhere in a controller a "$logger->info("foo");" and I see the trace from the log profiler page

Related

Symfony 6, Can't create a controller

I've installed Php 8 and I try to start coding with Symfony but I can't even create a controller.
I've created my project with:
create-project symfony/website-skeleton covid
The project was created with the version 6.1.99.
Then, in VScode, I used
symfony console make:controller Home
and it give me an error:
Parse error: syntax error, unexpected '?' in D:\Bureau\dev\covid\vendor\autoload_runtime.php on line 15
I don't understand why it's happening. Any idea ?
Edit:
It works fine if I use
php bin/console make:controller home
It seems I just can't use the symfony commands.

login failure in prodcution mode

I am using symfony 5.4.4.
I encounter a problem when I try to connect a user without launching the php server (I want to work in production mode).
I used make:user commands to create the User entity. Then I used the make:auth and make:registration-form commands to generate the login and registration forms.
everything works perfectly when I run the symfony server with the symfony serve command. On localhost:8000 I am able to register and login users with no problem.
I then followed the symfony documentation on deployment as I want my application to run in a server accessible from a private network. I then ran the commands:
composer require symfony/requirements-checker to check
composer install --no-dev --optimize-autoloader
Then I updated the .env file
APP_ENV=prod
APP_DEBUG=0
before clearing the cache with: php bin/console cache:clear
And finally I added rewrite rules with the command: composer require symfony/apache-pack.
now I access my application from any computer (example: http://localhost/myproject/public/user/4) in the network and I navigate without problems on the links.
However, when I try to login a user, it doesn't work.
And when I try to enter one, it works.
In summary, the login works on localhost:8000 but does not work on localhost/myproject/public while all other forms work.
Can you help me ? thank you in advance.
It's OK !
I added the method below into my App/src/Authenticator.php class
Notice : it does not work without the return type "bool" of the function
public function supports(Request $request): bool
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
}
I hope it will be useful to someone!
Thanks to all and especially to Cerad for the link !

Symfony 4 + graylog + console errors

I have a web application and I need to write exceptions to graylog. I asked sysadmins and they told me to just throw exception messages to stdout and it will be enough, but this way doesn't work for me.
Meanwhile I have next door virtualhost with native PHP app which just have error_reporting = E_ALL and display_errors = 1 and these settings are enough, and graylog see these errors, but not mine (I'm using Symfony 4 + Monolog) in the Monolog config I have almost the default settings
main:
type: fingers_crossed
level: debug
path: 'php://stdout'
If I change path from stdout to logfilename it works ok, but I need to see my logs in graylog, not in a file. Unfortunately I haven't got the graylog configs, the Symfony application ones only.

How to install mmoreramerino/GearmanBundle with Symfony 2.1.x?

I am pretty new to Symfony 2 and brand new to Gearman.
I am looking for a bundle to integrate Symfony 2 with Gearman.
mmoreramerino's bundle seems to be the most popular bundle according to packagist. Unfortunately something seems to be broken, the autoloader does not find the bundle.
Fatal error: Class 'Mmoreramerino\GearmanBundle\MmoreramerinoGearmanBundle' not found in ...
I tried switching to "dev-development" as I got from the issues that it was fixed in this branch, but it did not work for me as well.
Question: How can I install this bundle using Symfony 2.1.x?
Question 2: Are there any working & documented alternatives?
Edit In case someone else comes across this question: Here is how I got it up and running!
Install gearman, libgearman, the PECL extension for PHP (use recent versions!)
check that gearman shows up in phpinfo() (both cli and webserver version)
start gearmand in terminal 1 using "gearmand --verbose INFO" (you will see workers & clients connect to gearman - or not ;-))
start in terminal 2 reverse_worker.php from the gearman php extension example directory
start in terminal 3 reverse_client.php from the gearman php extension example directory
If this is working, you are ready for Symfony: install mmoreramerino/GearmanBundle using "dev-development"
copy dev.base.yml from the bundle to app/config/gearman/dev.yml
Now add TestWorker.php to your bundle as outlined in the documentation
enable the testWorker by using the console script "php app/console gearman:job:execute MmoreramerinoGearmanBundleWorkerstestWorker~test"
now you are able to send jobs to the listening testWorker in a Symfony controller (or somewhere else in Symfony). I had to specify the server though I am using the default host/port.
$gearman = $this->get('gearman');
$gearman->setServer('127.0.0.1',4730);
$gearman->doNormalJob('MmoreramerinoGearmanBundleWorkerstestWorker~test');
To install the bundle, you need to add the following line to composer.json
"Mmoreramerino/GearmanBundle": "dev-development"
and run composer update;
Then register it in app/AppKernel.php (it seems you have already done this)
new Mmoreramerino\GearmanBundle\MmoreramerinoGearmanBundle(),

Web setup project fails to install dynamic-data site: "the installer was interrupted"

The last phase of the installer fails with this message:
Installation Incomplete
The installer was interrupted before [project] could be installed. You need to restart the installer to try again.
Running msiexec /i installer.msi /l*vx setup.log shows the following entries in the setup log:
INFO : [...] [ApplyWebFolderProperties]: Getting web folder property token...
INFO : [...] [ApplyWebFolderProperties]: Token is '/LM/W3SVC/1/ROOT/ProjectDir/DynamicData/Filters'.
INFO : [...] [ApplyWebFolderProperties]: Getting METADATA_HANDLE for the directory '/LM/W3SVC/1/ROOT/ProjectDir/DynamicData/Filters'.
ERROR : [...] [ApplyWebFolderProperties]: FAILED: -2147024893
ERROR : [...] [ApplyWebFolderProperties]: FAILED: -2147024893
ERROR : [...] [ApplyWebFolderProperties]: Custom Action failed with code: '3'
ERROR : [...] [ApplyWebFolderProperties]: Custom Action failed with code: '3'
INFO : [...] [ApplyWebFolderProperties]: Custom Action completed with return code: '3'
The same web application had no problems being installed with a web setup project before. The issue started after upgrading the web application from .NET 3.5 SP1 to .NET 4.0.
This blog entry points out the issue:
Which got me started thinking, I have
a subfolder named filters. Changing
nothing else but renaming the filters
subfolder made it finish properly. I'm
assuming you might have the same
problems with folders named apppools,
info, or 1 as well.
(Emphasis mine)
Unfortunately, Filters is a hard-coded folder name in Dynamic Data. If you look at FilterFactory, there doesn't appear to be any way to override that value, seeing as how the FilterFactory property of MetaModel is not marked virtual. If we can't change the folder name, then we have to look at fixing the installer...
The installer error is being raised by the ApplyWebFolderProperties custom action. That action isn't built-in to Windows Installer—it's added by the Web Setup Project. That's helpful, because it means we can remove it with WiRunSQL.vbs:
cscript WiRunSQL.vbs installer.msi "DELETE FROM CustomAction WHERE Action='WEBCA_ApplyWebFolderProperties'"
Note that the actual name of ApplyWebFolderProperties is WEBCA_ApplyWebFolderProperties. Seeing as how the action doesn't appear to be documented anywhere, caveat emptor. It doesn't appear to be too terribly important though.
To automate the workaround, you could add the command to the setup project's PostBuildEvent like so:
cscript.exe "$(ProjectDir)..\WiRunSQL.vbs" "$(BuiltOuputPath)" "DELETE FROM CustomAction WHERE Action='WEBCA_ApplyWebFolderProperties'"
If anyone knows a better way to install a folder named Filters, I'd love to hear it.

Resources