Im making tests on a web server, showing a line if i go to a link within this server, it works fine but i wanted to implement monolog to check every time someone logs in and write it on a file. The code below
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/../../../vendor/autoload.php';
require __DIR__ . '/../utils/Requests.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logFilePath = __DIR__ . '/../../log/debug.log';
if (!file_exists($logFilePath)) {
file_put_contents($logFilePath, '');
}
try {
$logger = new Logger("daily");
$stream_handler = new StreamHandler($logFilePath, Logger::DEBUG);
$logger->pushHandler($stream_handler);
} catch (Exception $e) {
echo "Error al inicializar el logger: " . $e->getMessage();
}
class HomeService extends Requests
{
public function index()
{
global $logger;
$logger->debug("HomeService index method called");
$method = $this->getMethod();
$result = [];
if($method == 'GET') {
http_response_code(200);
$result = [
"message" => "Hey There! 🦍",
"guide" => "https://github.com/EricNeves/myBooksAPI"
];
} else {
$logger->debug("HTTP method not allowed: " . $method);
http_response_code(405);
$result['error'] = "HTTP Method not allowed";
}
$logger->debug("Response: " . json_encode($result));
echo json_encode($result, JSON_UNESCAPED_SLASHES);
}
}
$homeService = new HomeService();
$homeService->index();
works if i execute it through the console and it saves the log on the file but if i try to go to the link to call this funciton, it gives an error 500 and doesnt show nothing unless i comment everything logger related inside the index function, it gives back this error
<br />
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught Error: Call to a member function debug() on null in /var/www/html/prova_API/myBooksAPI-main/app/services/HomeService.php on line <i>33</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Error: Call to a member function debug() on null in /var/www/html/prova_API/myBooksAPI-main/app/services/HomeService.php on line <i>33</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0003</td><td bgcolor='#eeeeec' align='right'>362224</td><td bgcolor='#eeeeec'>{main}( )</td><td title='/var/www/html/prova_API/myBooksAPI-main/index.php' bgcolor='#eeeeec'>.../index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>364240</td><td bgcolor='#eeeeec'>Core->run( )</td><td title='/var/www/html/prova_API/myBooksAPI-main/index.php' bgcolor='#eeeeec'>.../index.php<b>:</b>35</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>367000</td><td bgcolor='#eeeeec'>require_once( <font color='#00bb00'>'/var/www/html/prova_API/myBooksAPI-main/app/services/HomeService.php</font> )</td><td title='/var/www/html/prova_API/myBooksAPI-main/app/core/Core.php' bgcolor='#eeeeec'>.../Core.php<b>:</b>33</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0038</td><td bgcolor='#eeeeec' align='right'>378264</td><td bgcolor='#eeeeec'>HomeService->index( )</td><td title='/var/www/html/prova_API/myBooksAPI-main/app/services/HomeService.php' bgcolor='#eeeeec'>.../HomeService.php<b>:</b>54</td></tr>
</table></font>
I tried giving all permissions to the file and directory but it doesnt work, i dont get why it works through console but not when i do it through the page
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/../../../vendor/autoload.php';
require __DIR__ . '/../utils/Requests.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class HomeService extends Requests
{
private $logger;
public function index()
{
$logFilePath = __DIR__ . '/../../log/debug.log';
if (!file_exists($logFilePath)) {
file_put_contents($logFilePath, '');
}
try {
$this->logger = new Logger("daily");
$stream_handler = new StreamHandler($logFilePath, Logger::DEBUG);
$this->logger->pushHandler($stream_handler);
} catch (Exception $e) {
echo "Error al iniciar el logger: " . $e->getMessage();
return;
}
$this->logger->debug("HomeService cridat");
$method = $this->getMethod();
$result = [];
if($method == 'GET') {
http_response_code(200);
$result = [
"message" => "Hey There! 🦍",
"guide" => "https://github.com/EricNeves/myBooksAPI"
];
} else {
$this->logger->debug("HTTP method not allowed: " . $method);
http_response_code(405);
$result['error'] = "HTTP Method not allowed";
}
$this->logger->debug("Response: " . json_encode($result));
echo json_encode($result, JSON_UNESCAPED_SLASHES);
}
}
$homeService = new HomeService();
$homeService->index();
?>
Thanks to Med for the answer