Cannot Log into stdout/sterr from Laravel Artisan Console in Docker Environment - php

I'm working on a project with PHP 7.1.33 and Laravel 5.2.45 (just a bit outdated I know) running on a Docker container. I used the following code in bootstrap/app.php in order to send different levels of logs to stdout/stderr:
if (in_array(env('APP_ENV'), ['staging', 'production'])) {
$app->configureMonologUsing(function($monolog) {
$bubble = false;
$debugStreamHandler = new Monolog\Handler\StreamHandler('php://stdout', Monolog\Logger::DEBUG, $bubble);
$monolog->pushHandler($debugStreamHandler);
$infoStreamHandler = new Monolog\Handler\StreamHandler('php://stdout', Monolog\Logger::INFO, $bubble);
$monolog->pushHandler($infoStreamHandler);
$noticeStreamHandler = new Monolog\Handler\StreamHandler('php://stdout', Monolog\Logger::NOTICE, $bubble);
$monolog->pushHandler($noticeStreamHandler);
$warningStreamHandler = new Monolog\Handler\StreamHandler('php://stdout', Monolog\Logger::WARNING, $bubble);
$monolog->pushHandler($warningStreamHandler);
$errorStreamHandler = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::ERROR, $bubble);
$monolog->pushHandler($errorStreamHandler);
$criticalStreamHandler = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::CRITICAL, $bubble);
$monolog->pushHandler($criticalStreamHandler);
$alertStreamHandler = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::ALERT, $bubble);
$monolog->pushHandler($alertStreamHandler);
$emergencyStreamHandler = new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::EMERGENCY, $bubble);
$monolog->pushHandler($emergencyStreamHandler);
});
}
Since this Laravel version does not have logging channel configuration, this one of a few ways to get it done in a global manner so I can use the Log facade.
When I Log from requests, everything works as expected (I also redirected Apache logs to stdout/stderr):
[name]#[name]:~ $ docker logs -f [name]
[23-Jan-2023 21:09:37] NOTICE: fpm is running, pid 1
[23-Jan-2023 21:09:37] NOTICE: ready to handle connections
[23-Jan-2023 22:04:56] WARNING: [pool www] child 8 said into stdout: "[2023-01-23 19:04:56] staging.INFO: testing from controller [] []"
172.30.0.9 - 23/Jan/2023:22:04:55 +0000 "POST /index.php" 200
But when I log from Console commands (executing them manually with php artisan while accessing the pod using "docker exec -it [name] /bin/bash"), the output only displays on the console or the screen of that session but it's not redirected like Controllers. I would like to understand why or if I'm missing something. I tried inspecting Laravel framework code, inside Monolog and Logger logic so I could find differences between Request and Console context but without success.
The main objective is to log some jobs that are executed by a PHP instance running with supervisor using "php artisan queue:work ..." and once captured by stdout/stderr, it would be processed by CloudWatch or any similar tool.
Thank you.

Related

Laravel Process can't find Node

I am developing a Laravel (8.83.24) app and testing with Laravel's built in server "php artisan serve". I am using Process and am trying to debug a memory allocation error which has lead me to here:
$process = new Process(['node', '-v']);
$process->run();
print_r($process);
That leads me to a line in the output:
[command] => cmd /V:ON /E:ON /D /C (node -v) 1>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.out" 2>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.err"
and sf_proc_01.err contains:
'node' is not recognized as an internal or external command,
operable program or batch file.
Clearly node can't be found. I am on Windows 10. Node is set in my in System and User PATH. Running "node -v" from cmd.exe works and returns the version number. So Laravel's server doesn't seem to be able to find node. However, I can't check the path as running this:
$process = new Process(['echo', '%PATH%']);
$process->run();
print_r($process);
Just leads to an output of
"%PATH%"
How can I make Node findable by Laravel / Process?
Many thanks for your help.
I eventually solved this by setting the path for node in my Laravel .env file:
NODE_PATH='C:\PROGRA~1\nodejs\node.exe'
NPM_PATH='D:\Work\Project\node_modules'

PHP fails to access the docker socket via curl

My Code fails to reach the Docker Socket
$client = new GuzzleHttp\Client();
$test = $client->request('GET','http://v1.40/containers/json',[
'curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']
]);
I only get a generic cURL error 7 from that and I´ve checked that the socket is available and working inside the Container with the cURL command from the cmd. Its only when i try to connect via PHP it fails ominously and frankly im out of ideas.
So just in case someone stumbles upon this in the future and has the same or a similar problem.
Guzzle was not the problem in this case but phpfpm. I did not realize that the phpfpm workers in the official php docker image used the www-data user by default. The way I used is to change the user in the www.conf (default /usr/local/etc/php-fpm.d/www.conf in docker)
user = root
group = root
you will have to append the -R flag to the run command to allow running the workers as root.

WSL2 Laravel dusk testing google-chrome driver unable to display site

Laravel Dusk, errors loading google-chrome
I am trying to run tests using Laravel Dusk on a Windows 10 subsystem, WSL2 running Ubuntu. I've followed the installation guides, and I'm stuck at the point after the google-chrome browser is loaded and renders the site:
This is my DuskTestCase.php driver method:
protected function driver()
{
$options = (new ChromeOptions)->addArguments(collect([
])->unless($this->hasHeadlessDisabled(), function ($items) {
return $items->merge([
'--disable-gpu',
// '--headless',
'--window-size=1920,1080'
]);
})->all());
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
Before running the test, I run chromedriver for linux
vendor/laravel/dusk/bin/chromedriver-linux
returns:
Starting ChromeDriver 99.0.4844.51 (d537ec02474b5afe23684e7963d538896c63ac77-refs/branch-heads/4844#{#875}) on port 9515
Next I run any test say AuthTest
php artisan dusk tests/Browser/AuthTest.php
The test cannot render the site: http://localhost:9515
Error in browser:
{"value":{"error":"unknown command","message":"unknown command: unknown command: login","stacktrace":"#0 0x55bf14b857d3 \u003Cunknown>\n#1 0x55bf148e1688 \u003Cunknown>\n#2 0x55bf14932e11 \u003Cunknown>\n#3 0x55bf14932b77 \u003Cunknown>\n#4 0x55bf148b81d4 \u003Cunknown>\n#5 0x55bf148b9020 \u003Cunknown>\n#6 0x55bf14bb649d \u003Cunknown>\n#7 0x55bf14bcf60c \u003Cunknown>\n#8 0x55bf14bb8205 \u003Cunknown>\n#9 0x55bf14bcfee5 \u003Cunknown>\n#10 0x55bf14bac070 \u003Cunknown>\n#11 0x55bf148b7d49 \u003Cunknown>\n#12 0x7f7d460390b3 \u003Cunknown>\n"}}
If I load google-chrome manually and try to go to the same address (http://127.0.0.1:9515/), the same error appears.
However if I close everything and run:
php artisan serve --port 9515
then
google-chrome
I am able to manually interact with the site, yet Laravel Dusk cannot load the browser.
The following error is thrown by Laravel Dusk
```bash
TypeError: Argument 1 passed to Facebook\WebDriver\Remote\DesiredCapabilities::__construct() must be of the type array, null given, called in /home/simon/sites/acquisitionboardgame_vue/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php o
n line 648
Can anyone point me in the right direction ?

laradock can't run shell_exec('git pull')?

This is the first time to add a gitlab webhook in my laravel application running in laradocker.
First, run docker up:
docker-compose up -d nginx redis mysql
Second, add webhook in my gitlab project
point to laravel website http://example.com/deploy/
Third, laravel add router and Controller
// web.php
Route::post('/deploy', 'DeployController#index')->name('deploy');
// DeployController
//........
$result = shell_exec("/usr/bin/git pull");
logger('success result: ' . $result);
//.........
It doesn't work!
which step go wrong?
I found php-fpm has logs like:
[22-Jan-2018 07:46:46] WARNING: [pool www] child 7 said into stderr: "sh: 1: /usr/bin/git: not found"
I am a new docker learner, it will helpfull if you leaves some comment or advise, thanks!
The error is clear, git is not installed or it is not in your $PATH.
/usr/bin/git: not found

GearmanClient::do(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed in php shell_exec

I am trying to run gearman client from php shell_exec but it always throw following error GearmanClient::do(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:485 in /var/www/html/client.php
But if i run it from terminal then it works but not from php shell_exec. Even i passed server name and port in addServer method
I am running on centos 6.2.
Client.php
$client= new GearmanClient();
$client->addServer('127.0.0.1',4730);
print $client->do("reverse","Testing");
worker.php
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1",4730);
$worker->addFunction("reverse", "my_reverse_function");
function my_reverse_function($job)
{
return strrev($job->workload());
}
Make sure your gearman packages are installed. Or manually installed if needed. Sometimes upgrading your system may cause your packages to be lost.
Second check your or launch it in debug mode as stated here http://gearman.org/getting-started/
gearmand -vvv
and then launch it run in the background
gearmand -d
I read on other questions treated to this matter that changes from
$woker->addServer('127.0.0.1',4730);
to
$worker->addServer('127.0.0.1:4730');
The same for $client->addServer('127.0.0.1',4730);

Categories