I migrate some web applications from PHP 5.6 to PHP 7.0 but I noticed that not handled exceptions won't be logged in the configured log (or at least in the apache log error.log). The error message won't be displayed on the web site, it just stops after the error has been raised.
I tried it with that simple code:
<?php
namespace Application\MyApp;
ini_set('error_log', '/my/log/location/my_error.log');
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$host = gethostname();
echo "<b>start test on $host</b><br>";
throw new \Error('Test exception outside of try/catch.');
echo "<b>test finished on $host<b><br>";
With PHP 5.6 I get with the same code the expected error message in the configured log:
PHP Fatal error: Uncaught exception 'Error' with message 'Test exception outside of try/catch.
Is it possible to redirect all uncaught exceptions into the configured log file, as in PHP 5.6?
I get the required logging massages, if I add a exception handler method:
function myExceptionHandler (\Throwable $ex)
{
echo $ex->getMessage();
throw $ex;
}
set_exception_handler('Application\MyApp\myExceptionHandler');
Related
Have a xamp server running PHP Version 8.1.10. When trying to connect, it gives the following error: Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\elims\databaseConnect.php:7 Stack trace: #0 C:\xampp\htdocs\elims\index.php(7): require_once() #1 {main} thrown in C:\xampp\htdocs\elims\databaseConnect.php on line 7
Commenting out the following lines with # logged me in the main window of the application but failed to connect with the sql batabes:
<?php
$host='localhost';
$username='root';
$password='';
$database='elims';
**#** $connect=mysql_connect($host,$username,$password) or die("databse connection failed...".mysql_error());;
**#** $db_select=mysql_select_db($database,$connect) or die("databse connection failed...".mysql_error());
?>
Do understand mysql_connect is nolonger applicatble in php versions higher than 7, then what works.
After Installing the moodle website(Moodle Version: 3.8.1), on opening the login page(/login/index.php). I am getting below error.
Exception - Call to undefined method theme_boost\output\core_renderer::get_setting()
Please let me know what are the changes require.. in framework or in configurations
I would like to know how I can make monolog for symfony 4 logging fatal errors:
PHP Fatal error: Allowed memory size of xxxx exhausted
to the main log handler.
In my application these errors are logged in the PHP error_log file.
You can catch fatal error in PHP7, with Throwable exception. https://www.php.net/manual/en/class.throwable.php
This answer can help you https://stackoverflow.com/a/44280053/4939326.
I am not sure why this isn't working. Here is the code it is referencing.
function db_connection($query) {
mysql_connect('127.0.0.1', 'runner_db_user', 'runner_db_password')
OR die(fail('Could not connect to database.'));
mysql_select_db('ntc_race_info');
return mysql_query($query);
Make sure your installation of PHP has been compiled with mysql support. Run a phpinfo() to see if the php_mysql extension module is loaded. Create a test web page containing
<?php
phpinfo();
?>
then run it,If you don't see page for MySQL, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in
or use mysqli_connect() instead of mysql_connect()
$con = mysqli_connect("127.0.0.1","runner_db_user","runner_db_password","ntc_race_info");
I have a connection to a data base and every time there is a problem connecting I get this error.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on '205.178.146.104' (4)' in /data/18/2/77/115/2566441/user/2813515/htdocs/ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Abstract.php:129
Stack trace:
#0 .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Abstract.php(129): PDO->__construct('mysql:dbname=si...', 'xxx', 'xxx', Array)
#1 .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect()
#2 .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Abstract.php(459): Zend_Db_Adapter_Pdo_Mysql->_connect()
#3 .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('DESCRIBE `user`', Array)
#4 .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Mysq in .../ZendFramework-1.11.5/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144
The thing is that the database user and password are visible on the page in the error message. Is there a way that I can prevent PHP from showing that info? Or is this happening because I am set up a a developing instead of production?
It is a combination of both. In development, your showing your errors. Which is the primary reason your seeing that.
However, you can catch the exception by testing the connection before the query. Allowing you to handle your own connection failures.
try {
Zend_Db_Table::getDefaultAdapter()->getConnection();
} catch (Zend_Exception $e) {
throw new Zend_Exception("A different error");
}
It is because you are on "developing" (exceptions are shown in browser)
Solution 1: Use Production Environment ;)
Solution 2: Change settings in application.ini
Solution 3: Use an try/catch block for database connect